ggplot2|主题相关函数,美化图形,总有一款适合你
专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。
【R语言】开通了R语言群,大家相互学习和交流,请扫描下方二维码,备注:姓名-R群,我会邀请你入群,一起进步和成长。
本文收录一些ggplot2包画图相关的主题函数,使用这些主题相关函数,可以美化你的图形,让图形的风格更加优雅。幸运的是,已经有很多设计和构建好的主题供我们使用,我们只需要在自己的图形上,选择适合自己主题即可。
这些主题相关函数集,需要用到以下R包。
1 ggplot2包
2 ggthemes包
3 hrbrthemes包
4 egg包
5 ggpubr包
6 bigstatsr包
我们编写R语言程式,演示如下。
首先,加载所需R包
# R包
library(ggplot2)
library(ggthemes)
library(hrbrthemes)
library(egg)
library(ggpubr)
library(bigstatsr)
其次,加载数据和数据理解
# 数据加载
data(iris)
# 数据理解
dim(iris)
str(iris)
summary(iris)
接下来,绘制基本图形。
任务:探索萼片长度与萼片宽度之间的关系。
两者都是连续变量,我们采用散点图,并且利用类别进行点的颜色和形状控制。
p <- ggplot(iris, aes(x=Sepal.Length,
y=Sepal.Width,
color=Species,
shape=Species)) +
geom_point(size=3, alpha=0.6, show.legend = FALSE)
# 图片四周到边缘距离
mytheme <- theme(
plot.margin=unit(rep(1.2,4),"cm")
)
p + mytheme
第一、使用ggplot2包自带的主题函数
1 theme_bw()主题
p + theme_bw() + mytheme
2 theme_minimal()主题
p + theme_minimal() + mytheme
3 theme_classic() 主题
p + theme_classic() + mytheme
4 theme_gray() 主题
p + theme_gray() + mytheme
第二、使用ggthemes包提供的主题函数
1 theme_excel()主题
p + theme_excel() + mytheme
2 theme_economist()主题
p + theme_economist() + mytheme
3 theme_fivethirtyeight()主题
p + theme_fivethirtyeight() + mytheme
4 theme_tufte()主题
p + theme_tufte() + mytheme
5 theme_gdocs()主题
p + theme_gdocs() + mytheme
6 theme_wsj()主题
p + theme_wsj() + mytheme
7 theme_cal()主题
p + theme_calc() + scale_colour_calc() + mytheme
8 theme_hc() 主题
p + theme_hc() + scale_colour_hc() + mytheme
第三、使用hrbrthemes包提供的主题函数
hrbrthemes包预先构建好的主题函数,如下图所示
p + theme_ipsum() + scale_color_ipsum() + mytheme
p + hrbrthemes::theme_modern_rc() + scale_color_ipsum() + mytheme
第四、使用egg包提供的主题函数
egg包预先构建好的主题函数,如下图所示
p + theme_article() + mytheme
p + egg::theme_presentation() + mytheme
第五、使用ggpubr包提供的主题函数
gggpubr包提供的主题函数,如下图所示
p + theme_pubclean() + mytheme
p + ggpubr::theme_cleveland() + mytheme
p + ggpubr::theme_pubr() + mytheme
其它主题,你可以亲自尝试和感受一下。
第六、使用bigstatsr包提供的主题函数
1 theme_bigstatsr()主题
p + theme_bigstatsr() + mytheme
伙伴们,这些主题函数,我们建议你亲自去尝试和体验一下,选择适合自己图形的主题,从而让图形更亮彩。
本文完整的R代码
####################
#主题相关的函数,美化图形
###################
# R包
library(ggplot2)
library(ggthemes)
library(hrbrthemes)
library(egg)
library(ggpubr)
library(bigstatsr)
# 数据加载
data(iris)
# 数据理解
dim(iris)
str(iris)
summary(iris)
# 基本图形
# 探索Sepal.Length和Sepal.Width的关系
p <- ggplot(iris, aes(x=Sepal.Length,
y=Sepal.Width,
color=Species,
shape=Species)) +
geom_point(size=6, alpha=0.6, show.legend = FALSE)
# 图片四周到边缘距离
mytheme <- theme(
plot.margin=unit(rep(1.2,4),"cm")
)
p + mytheme
# 第一:使用ggplot2包自带的主题函数
# 1)theme_bw()
p + theme_bw() + mytheme
# 2)theme_minimal()
p + theme_minimal() + mytheme
# 3)theme_classic()
p + theme_classic() + mytheme
# 4)theme_gray()
p + theme_gray() + mytheme
# 第二:使用ggthemes包提供的主题函数
# 1)theme_excel()
p + theme_excel() + mytheme
# 2) theme_economist()
p + theme_economist() + mytheme
p + theme_economist_white() + mytheme
# 3) theme_fivethirtyeight()
p + theme_fivethirtyeight() + mytheme
# 4) theme_tufte()
p + theme_tufte() + mytheme
# 5) theme_gdocs()
p + theme_gdocs() + mytheme
# 6) theme_wsj()
p + theme_wsj() + mytheme
# 7) theme_calc()
p + theme_calc() + scale_colour_calc() + mytheme
# 8) theme_hc()
p + theme_hc() + scale_colour_hc() + mytheme
# 第三:使用hrbrthemes包提供的主题函数
# hrbrthemes::theme_ipsum_pub()
# hrbrthemes::theme_ipsum_es()
# hrbrthemes::theme_ipsum()
# hrbrthemes::theme_ft_rc()
# hrbrthemes::theme_modern_rc()
# 等等
# 1) theme_ipsum()
p + theme_ipsum() + scale_color_ipsum() + mytheme
p + hrbrthemes::theme_ipsum_pub() + scale_color_ipsum() + mytheme
p + hrbrthemes::theme_modern_rc() + scale_color_ipsum() + mytheme
# 第四:使用egg包提供的主题函数
# egg::theme_article()
# egg::theme_presentation()
# 1)theme_article()
p + theme_article() + mytheme
p + egg::theme_presentation() + mytheme
# 第五:使用ggpubr包提供的主题函数
# ggpubr::theme_pubclean()
# ggpubr::theme_classic2(
# )
# ggpubr::theme_cleveland()
# ggpubr::theme_pubr()
# ggpubr::theme_transparent()
# 1)theme_pubclean()
p + theme_pubclean() + mytheme
p + ggpubr::theme_cleveland() + mytheme
p + ggpubr::theme_pubr() + mytheme
# 第六:使用bigstatsr包提供的主题函数
# 1)theme_bigstatsr()
p + theme_bigstatsr() + mytheme
参考资料:
1https://www.r-graph-gallery.com/192-ggplot-themes.html
如何高效学习?伙伴们可以看下这个视频。
好书推荐
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!