• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      未分类

      • 首页
      • 博客
      • 未分类
      • ggplot2|主题相关函数,美化图形,总有一款适合你

      ggplot2|主题相关函数,美化图形,总有一款适合你

      • 发布者 weinfoadmin
      • 分类 未分类
      • 日期 2021年9月9日
      • 评论 0评论

      专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。

      是新朋友吗?记得先点R语言关注我哦~
      《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


      如何高效学习?伙伴们可以看下这个视频。

      好书推荐

      1 R语言做商业智能,助你提高商业生产率

      2 用R、tidyverse和mlr做机器学习

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      ggplot2包|创建美丽有用的折线图
      2021年9月9日

      下一篇文章

      R For Everyone,人人都可学R和用R,以发现数据里的价值
      2021年9月9日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      8-1678501786
      肿瘤细胞通过改变CD8+ T细胞中的丙酮酸利用和琥珀酸信号来调控抗肿瘤免疫应答。
      7 12月, 2022

      留言 取消回复

      要发表评论,您必须先登录。

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?