ggplot2|主题相关函数,美化图形,总有一款适合你
卖萌控的博客
点击这里进入电脑版页面!体验更好
ggplot2|主题相关函数,美化图形,总有一款适合你
2022-6-23 萌小白


《R语言实践》专栏·第10篇



| RUser



1671字 |4分钟阅读




【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



发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容