## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.2 √ purrr 0.3.4
## √ tibble 3.0.3 √ dplyr 0.8.5
## √ tidyr 1.0.2 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Registering fonts with R
## --- Compiling #TidyTuesday Information for 2020-09-01 ----
## --- There are 5 files available ---
## --- Starting Download ---
##
## Downloading file 1 of 5: `arable_land_pin.csv`
## Downloading file 2 of 5: `cereal_crop_yield_vs_fertilizer_application.csv`
## Downloading file 3 of 5: `cereal_yields_vs_tractor_inputs_in_agriculture.csv`
## Downloading file 4 of 5: `key_crop_yields.csv`
## Downloading file 5 of 5: `land_use_vs_yield_change_in_cereal_production.csv`
## --- Download complete ---
## --- Compiling #TidyTuesday Information for 2020-09-01 ----
## --- There are 5 files available ---
## --- Starting Download ---
##
## Downloading file 1 of 5: `arable_land_pin.csv`
## Downloading file 2 of 5: `cereal_crop_yield_vs_fertilizer_application.csv`
## Downloading file 3 of 5: `cereal_yields_vs_tractor_inputs_in_agriculture.csv`
## Downloading file 4 of 5: `key_crop_yields.csv`
## Downloading file 5 of 5: `land_use_vs_yield_change_in_cereal_production.csv`
## --- Download complete ---
glimpse() is like a transposed version of print(),It’s a little like str() applied to a data frame but it tries to show you as much data as possible.
## Rows: 13,075
## Columns: 14
## $ Entity <chr> "Afghanistan", "Afghanistan", "A...
## $ Code <chr> "AFG", "AFG", "AFG", "AFG", "AFG...
## $ Year <dbl> 1961, 1962, 1963, 1964, 1965, 19...
## $ `Wheat (tonnes per hectare)` <dbl> 1.0220, 0.9735, 0.8317, 0.9510, ...
## $ `Rice (tonnes per hectare)` <dbl> 1.5190, 1.5190, 1.5190, 1.7273, ...
## $ `Maize (tonnes per hectare)` <dbl> 1.4000, 1.4000, 1.4260, 1.4257, ...
## $ `Soybeans (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ `Potatoes (tonnes per hectare)` <dbl> 8.6667, 7.6667, 8.1333, 8.6000, ...
## $ `Beans (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ `Peas (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ `Cassava (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ `Barley (tonnes per hectare)` <dbl> 1.0800, 1.0800, 1.0800, 1.0857, ...
## $ `Cocoa beans (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ `Bananas (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, ...
NZ_yield <- key_crops %>%
filter(Code == "NZL") %>% # 筛选Code等于NZL的
pivot_longer(cols = 4:last_col(),#将数据从宽类型转换成长数据,这是因为ggplot2要求的,如果要画多类别的数据,转换成长数据是最方便的
names_to = "crop", # 将转换为类别的这一列的名字改为“crop”
values_to = "crop_production") %>% # 将值这一列的名字改为cropproduction
mutate(crop = str_remove_all(crop, " \\(tonnes per hectare\\)")) %>% # 将数据框的crop列中含有(tonnes per hectare)都删掉
set_names(nm = names(.) %>% tolower()) %>% # 将数据框的列名字都改为小写的
filter(!is.na(crop_production)) # 筛选crop——production为空的
也就是用来控制ggplot2的布局、字体、背景等
#Set theme
font_family1 <- 'Times New Roman' #'Century Gothic' # 这里设置默认字体为新罗马
font_family2 <- 'Times New Roman'
background <- "#39393A" # 设置背景颜色
text_colour1 <- "white" # 设置文本1的颜色
text_colour2 <- "black" # 设置文本2 的颜色
axis_colour <- "white" # 设置参考线的颜色(注意我这里说的是参考线,并不是坐标轴)
theme_style <- theme(text = element_text(family = font_family1),
#这里是设置ggplot2的全部字体
rect = element_rect(fill = background),
#这里是设置所有的矩形元素填充颜色
plot.background = element_rect(fill = background, color = NA),
#设置背景颜色
plot.title = element_text(size = 30, colour = text_colour1),
#设置标题的字体 size为d大小,colour为颜色
plot.subtitle = element_text(size = 16, colour = text_colour1),
#副标题
plot.caption = element_text(size = 12, colour = text_colour1,
margin=margin(60,0,0,0)),
# 右下角的标注
panel.background = element_rect(fill = background, color = NA),
#设置面板背景(也就是绘图区域的背景)
panel.border = element_blank(), # 绘图区域的边框
panel.grid.major.y = element_blank(), # 绘图区域的网格
panel.grid.major.x = element_blank(), # 绘图区域x主网格
panel.grid.minor.x = element_blank(), #绘图区域的x次要网格
plot.margin = unit(c(1, 1, 1, 1), "cm"), # ggplot2的边框
axis.title = element_blank(), # 设置x,y轴的标签为空
axis.text.x = element_text(size = 14, colour= text_colour1),
#设置x轴刻度的标签
axis.text.y = element_blank(), # 设置y轴刻度的标签为空
axis.line = element_blank(), # 设置轴的线为空
legend.text = element_text(size = 10, colour= text_colour1),
#设置图例的字体
legend.title = element_blank(), # 图例的名字
legend.box = "horizontal", # 图例的摆放方向
legend.position="top", # # 图例的摆放位置
legend.justification ="left")
#用来固定图例的位置,在画图区域内还算画图区域外
theme_set(theme_classic() + theme_style) # 设置为全局的主题
#Set colour palette
cols <- c("#F3D2B3", "#F2B8A2", "#F38C8D", "#5E9DB8", "#ADA296", "#2C5F72")
#Plot data
ggplot(NZ_yield, aes(year, crop_production, fill = factor(crop))) +
geom_stream(method = "raw", bw = .7) +
scale_fill_manual(name = "crop", values = cols) + # 设置填充颜色
scale_x_continuous(breaks = seq(1960, 2020, 10)) + # 设置x轴的分割
geom_vline(data = tibble(x = c(1960, seq(1970, 2010, by = 10), 2020)),
aes(xintercept = x), color = axis_colour, size = .1) + #因为网格都被取消了,现在需要使用geom——vline来模拟网格,哈哈哈,我也经常干
geom_segment(aes(x=1961,xend=1982,y=45,yend=45),size = .5, color="white", linetype = 2) + # 注意到文字的上的虚线。这个就是画虚线的
annotate("label", x = 1971, y = 45, size = 6, fontface = "italic", label.size=NA,
color = text_colour1, family = font_family2, fill = background,
label = "Golden Years of Agriculture") + # 添加文本注释
geom_segment(aes(x=1984,xend=2018,y=45,yend=45),color="white", linetype = 2) +
annotate("label", x = 2001, y = 45, size = 6, fontface = "italic", label.size=NA,
color = text_colour1, family = font_family2, fill = background,
label = "Deregulation and Export Growth") +
geom_curve(aes(x = 1975, y = -35, xend = 1975, yend = -17),
arrow = arrow(length = unit(0.3, "cm")), size = 0.5,
color = "white", curvature = -0.3) + # 添加曲线,就是左下角的带有➡️的
annotate("label", x = 1975, y = -35, size = 3.5, color = text_colour1, family = font_family1, fill = background,
label =
"Trials for growing soybeans occurred\nin the 1970s, but evidence suggested it\nwasn't going to be a commercially\nviable crop"
) +
annotate("text", x = 1965.5, y = 0, size = 3.5, color = text_colour2, family = font_family1,
label =
"Majority of potatoes were\nsold fresh and prepared\nat home during the 1960s"
) +
annotate("text", x = 1995, y = 0, size = 3.5, color = text_colour2, family = font_family1,
label =
"Exporting Potatoes began\nin 1991 and have steadily\ngrown since then"
) +
annotate("text", x = 2005, y = 5, size = 3.5, color = text_colour2, family = font_family1,
label = "By 2007 over half of NZ's\npotato crop was processed\ninto chips and fries") +
annotate("text", x = 2005, y = -15, size = 3.5, color = text_colour2, family = font_family1,
label = "Additionally, Potatoes were\nNZ's second highest earning\nvegetable in 2007 (NZ$94.2M).\nOnions took the crown as the\nhighest earner") +
labs(title = "Kiwis and their Potatoes",
subtitle = "New Zealand Crop Yields (tonnes per hectare) Since 1961",
caption = "Visualisation: @JaredBraggins | Sources: Our World in Data, Te Ara") +
guides(fill = guide_legend(nrow = 1)) # 控制图例的排数。,就一排
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database