在使⽤ggplot2画图的时候如何导⼊字体,在使⽤ggplot2的时候如何处理复杂的控件、主题等,今天将⼀⼀介绍。
ggplot2使⽤字体。
成都水族馆
这个难点肯定就是导⼊字体,然后在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(), # 绘图区域的边框
plot.margin = unit(c(1, 1, 1, 1), "cm"), # ggplot2的边框
五圣
axis.title = element_blank(), # 设置x,y轴的标签为空
< = element_text(size = 14, colour= text_colour1), # 设置x轴刻度的标签
< = element_blank(), # 设置y轴刻度的标签为空
axis.line = element_blank(), # 设置轴的线为空
< = element_text(size = 10, colour= text_colour1), # 设置图例的字体
legend.title = element_blank(), # 图例的名字
legend.box = "horizontal", # 图例的摆放⽅向
legend.position="top", # # 图例的摆放位置
legend.justification = "left") # ⽤来固定图例的位置,在画图区域内还算画图区域外
theme_t(theme_classic() + theme_style) # 设置为全局的主题
第六部分是画图(也就是将数据传递给ggplot2)
#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 = q(1960, 2020, 10)) + # 设置x轴的分割
geom_vline(data = tibble(x = c(1960, q(1970, 2010, by = 10), 2020)),
aes(xintercept = x), color = axis_colour, size = .1) + # 因为⽹格都被取消了,现在需要使⽤geom——vline来模拟⽹格,哈哈哈,我也经常⼲
geom_gment(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_gment(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 occurrednin the 1970s, but evidence suggested itnwasn't going to be a commerciallynviable crop"
) +
健康小报>海参蛋羹
annotate("text", x = 1965.5, y = 0, size = 3.5, color = text_colour2, family = font_family1,
洞穴隐喻
label =
"Majority of potatoes werensold fresh and preparednat home during the 1960s"
) +
annotate("text", x = 1995, y = 0, size = 3.5, color = text_colour2, family = font_family1,
label =
"Exporting Potatoes begannin 1991 and have steadilyngrown since then"
) +
annotate("text", x = 2005, y = 5, size = 3.5, color = text_colour2, family = font_family1,
label = "By 2007 over half of NZ'snpotato crop was procesdninto chips and fries") +
annotate("text", x = 2005, y = -15, size = 3.5, color = text_colour2, family = font_family1,
label = "Additionally, Potatoes werenNZ's cond highest earningnvegetable in 2007 (NZ$94.2M).nOnions took the crown as thenhighest 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)) # 控制图例的排数。,就⼀排
第七部分保存图⽚。
#Export plot
ggsave("NZ Crops.png", dpi = 700, width = 15, height = 9)
关于保存图像缩放⽐例的,可以看我的另外⼀个⽂章:
圆⼦:关于ggplot2缩放问题的解决z