简介
本次推文参考书籍《THE HITCHHIKER’S GUIDE TO GGPLOT2[1]》并结合小编个人科研经验。主要目的:带大家一步步绘制以下条形图:
类似的文章小编还写过:如何一步步提高图形 B 格?以 ggplot 绘图为例。
数据介绍
使用以下数据作为案例,数据预览如下所示
library(ggplot2) library(ggthemes) library(dplyr) library(readr) library(scales) library(forcats) chilean.exports = "year,product,export,percentage 2006, copper,4335009500,81 2006,others, 1016726518,19 2007,copper,9005361914,86 2007,others,1523085299,14 2008, copper,6907056354,80 2008, others, 1762684216, 20 2009,copper, 10529811075,81 2009,others,2464094241,19 2010, copper, 14828284450,85 2010,others,2543015596,15 2011,copper,15291679086,82 2011, others, 3447972354,18 2012, copper,14630686732,80 2012,others,3583968218,2( 2013,copper,15244038840,79 2013,others,4051281128,21 2014,copper,14703374241,78 2014,others, 4251484600,22 2015, copper,13155922363,78 2015, others, 3667286912, 22 " charts.data = read_csv(chilean.exports)
数据预览
条形图
接下来根据该数据绘制条形图。
基础图形
以年份为 x 轴,出口量为 y 轴,产品分类作为填充颜色,来绘制条形图(geom_col()
)。
p3 = ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = charts.data) + geom_col() p3
增加数据标签
先使用 mutate()
添加标签数据,之后在原图基础上添加数据标签(geom_text()
)。
charts.data = charts.data %>% mutate(export_label = paste(round(export/1000000000,2), "B")) p3 = p3 + geom_text(data = charts.data, aes(x = year, y = export, label = export_label), size = 3) p3
调整数据标签位置
由于默认加入的标签位置不合适,所以通过参数 position = position_stack(vjust = 0.5)
进行修改标签位置。
p3 = ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = charts.data) + geom_col() p3 = p3 + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), size = 3) p3
调整图例位置
调整图例位置,从默认的右侧转变为底部 (legend.position = "bottom"
),并设置标签方向为水平(legend.direction = "horizontal"
),取消标签标题(legend.title = element_blank()
)。
p3 = p3 + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) p3
调整 x 轴刻度
x 轴坐标为年份,所以进行刻度设置。
p3 = p3 + scale_x_continuous(breaks = seq(2006,2015,1)) p3
调整坐标轴标签和添加标题
p3 = p3 + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") p3
调整颜色配色
类似于推文:如何一步步提高图形 B 格?以 ggplot 绘图为例。前面的图形是基于 ggplot[2] 默认配色绘制的,如果你想使得图形更加上档次,可以使用其他配色方案。可以使用其他 R 包提供的配色方案,例如:ggsci[3],viridis[4]等。当然也可以使用 scale_fill_manual()
手动添加颜色。
fill = c("#E1B378","#5F9EA0") p3 = p3 + scale_fill_manual(values = fill) p3
修改字体类型
读者可以通过修改字体类型来增加图形的档次。主要使用 showtext[5] 包来实现该功能。假设我们使用 Google 的字体。在代码中,主要通过参数 element_text(family = "bell"),family = "bell"
等方式设置字体类型。此时得到的图形如下所示:
library(showtext) font_add_google("Gochi Hand", "gochi") font_add_google("Schoolbell", "bell") showtext_auto() fill = c("#4702A1","#227CC9") p3 = ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = charts.data) + geom_col() + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), size = 3, family = "bell", colour = "white", show.legend = F) + scale_x_continuous(breaks = seq(2006,2015,1)) + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") + scale_fill_manual(values = fill) + theme(axis.line.x = element_line(linewidth = .5, colour = "black"), axis.line.y = element_line(size = .5, colour = "black"), axis.text.x = element_text(colour = "black", size = 10), axis.text.y = element_text(colour = "black", size = 10), legend.key = element_rect(fill = "white", colour = "white"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), plot.title = element_text(family = "bell"), text = element_text(family = "gochi")) + guides(fill = guide_legend(reverse = T)) p3
使用自定义主题
为了复现前文介绍的图形,我们可以次啊用自定义主题的方式。自己设置填充颜色,以及主题中的细节设定。
fill = c("#b2d183","#40b8d0") p3 = ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = charts.data) + geom_col() + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), colour = "black", family = "Tahoma", size = 3, show.legend = F) + scale_x_continuous(breaks = seq(2006,2015,1)) + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") + scale_fill_manual(values = fill) + theme(panel.border = element_rect(colour = "black", fill = NA, linewidth = .5), axis.text.x = element_text(colour = "black", size = 10), axis.text.y = element_text(colour = "black", size = 10), legend.key = element_rect(fill = "white", colour = "white"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(), panel.grid.major = element_line(colour = "#d3d3d3"), panel.grid.minor = element_blank(), panel.background = element_blank(), plot.title = element_text(size = 14, family = "Tahoma", face = "bold"), text = element_text(family = "Tahoma")) + guides(fill = guide_legend(reverse = T)) p3
小编有话说
- 其他图形的绘制与其类似,大致就是通过这几种方式来美化图形。
参考资料
[1]
THE HITCHHIKER’S GUIDE TO GGPLOT2: https://leanpub.com/ggplot-guide
[2]
ggplot: https://ggplot2.tidyverse.org/reference/ggplot.html
[3]
ggsci: https://github.com/nanxstats/ggsci
[4]
viridis: https://github.com/sjmgarnier/viridis
[5]
showtext: https://cran.rstudio.com/web/packages/showtext/vignettes/introduction.html