学习笔记Day6:R语言作图—ggplot2

R语言作图-ggplot2

画图是展示数据的方法

常用可视化R包和函数

作图:base、ggplot2、ggpubr

拼图:par里的mfrow、gridarrange、cowplot、patchwork

导出:经典三段论、ggsave、eoffice(topptx)

作图

基础包绘图

在这里插入图片描述

高级绘图函数+补充的低级绘图函数(可选)=完整图

plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello')             ##紧接着运行即可加在图上

在这里插入图片描述

ggplot2
library(ggplot2)
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

在这里插入图片描述

ggpubr

是ggplot2的简化版本,沿用其另一种默认设置,新手友好

library(ggpubr)
ggscatter(iris,
          x="Sepal.Length",
          y="Petal.Length",
          color="Species")

在这里插入图片描述

ggplot2-绘图模板

  1. 绘图模板:ggplot(数据)+FUNCTION(mapping= aes(映射))+属性

    #模板
    ggplot(data = <DATA>)+
      geom_FUNCTION(mapping = aes(<MAPPING>))
    #代码
    ggplot(data = iris)+
      geom_point(mapping = aes(x = Sepal.Length,
                               y = Petal.Length))
    

    特殊语法:行末写+,列名单独出现,不加""

  2. 属性设置:是geom_FUNCTION的参数

    • 颜色:color
    • 大小:size
    • 形状:shape
    • 透明度:alpha
    • 填充颜色:fill
    ggplot(data = iris) + 
      geom_point(mapping = aes(x = Sepal.Length, 
                               y = Petal.Length), 
                 size = 6,     # 点的大小5mm
                 alpha = 0.3,  # 透明度 50%
                 shape = 20,
                 color = 'orangered')  # 点的形状
    

    在这里插入图片描述

  3. 映射:按照数据框的某一列来定义图的某个属性,是aes()的参数

    • x轴:x
    • y轴:y
    • 颜色:color
    • 大小:size
    • 形状:shape
    • 透明度:alpha
    • 填充颜色:fill
    ggplot(data = iris)+
      geom_point(mapping = aes(x = Sepal.Length,
                               y = Petal.Length,
                               color = Species))
    

    在这里插入图片描述

    • 如何自行指定映射的具体颜色

      ggplot(data = iris)+
        geom_point(mapping = aes(x = Sepal.Length,
                                 y = Petal.Length,
                                 color = Species))+
        scale_color_manual(values = c("blue","grey","red"))
      

      在这里插入图片描述

    • 配色R包rcolorBrewer已经内置进ggplot2

      ggplot(data = iris)+
        geom_point(mapping = aes(x = Sepal.Length,
                                 y = Petal.Length,
                                 color = Species))+
        scale_color_brewer(palette = 'Set1')
      

      在这里插入图片描述

      更多样的颜色:使用十六进制颜色编码

    • 区分color和fill两个属性

      • 实心图形:使用color控制颜色

      • 空心图形:color控制外框,fill控制填充

        ggplot(data = iris)+
          geom_point(mapping = aes(x = Sepal.Length,
                                   y = Petal.Length),
                     shape = 24,
                     fill = "yellow",
                     color = 'red')
        

        在这里插入图片描述

  4. 几何对象:以geom_funciton()画出的图形均称为几何对象

    几何对象可以叠加,类似ps图层的概念

    ##局部设置,对所在的图层/几何对象生效
    ggplot(data = iris) + 
      geom_smooth(mapping = aes(x = Sepal.Length, 
                              y = Petal.Length))+
      geom_point(mapping = aes(x = Sepal.Length, 
                               y = Petal.Length))
    ##全局设置,对全部的图层/几何对象生效
    ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+
      geom_smooth()+
      geom_point()
    

    在这里插入图片描述

  5. 位置:

    ggplot(data = iris,mapping = aes(x = Species,
                                     y = Sepal.Width,
                                     fill = Species))+
      geom_boxplot()+
      geom_point()
    #
    ggplot(data = iris,mapping = aes(x = Species,
                                     y = Sepal.Width,
                                     fill = Species))+
      geom_boxplot()+
      geom_point(position = 'jitter')/#geom_jitter()
    

    在这里插入图片描述
    在这里插入图片描述

    jitter的替代—dotplot和蜜蜂图

  6. 坐标系:coord_flip()反转坐标系。

  7. 主题:ggplot2有很多种主题,如+theme_bw()可以将背景改为白色。

  • 图层的叠放顺序:先写在下,后写在上

    ggplot(data = iris, mapping = aes(x = Species,
                                      y = Sepal.Width))+
      geom_violin(mapping = aes(fill = Species))+
      geom_boxplot()+
      geom_point(mapping = aes(shape = Species),position='jitter')+
      coord_flip()
    

    在这里插入图片描述

ggpubr

搜代码直接使用即可,不需要系统学习

代码示例:

# sthda上有大量ggpubr出的图
library(ggpubr)
p = ggboxplot(iris, x = "Species", y = "Sepal.Length",
              color = "Species", shape = "Species",add = "jitter")
p

在这里插入图片描述

ggpubr的实用小功能:实现组间比较

#要求是长度为2的向量组成的列表,内容是由横坐标组成。
my_comparisons <- list( c("setosa", "versicolor"), 
                        c("setosa", "virginica"), 
                        c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons,
                       aes(label = after_stat(p.signif)))

在这里插入图片描述

  • ggpubr和ggplot2的图可以赋值,基础包画图不可赋值。

图片保存

  1. ggplot2系列的保存方法:

    ggsave(p,filename = "iris_box_ggpubr.png")  
    #文件类型根据后缀决定
    #一些参数支持修改分辨率、长宽比例等,有需要时详细学习。
    
  2. 通用保存方法:三段论

    • 第一段:保存函数及文件名

    • 第二段:作图代码

    • 第三段:关闭画板

      #第一段
      pdf("iris_box_ggpubr.pdf")
      #第二段
      boxplot(iris[,1]~iris[,5])
      text(6.5,4, labels = 'hello')
      #第三段
      dev.off()
      
  3. eoffice包:将图片保存至ppt

    library(eoffice)
    topptx(p,"iris_box_ggpubr.pptx")
    

拼图

patchwork包:完美兼容ggplot2

  1. 支持p1+p2直接拼图
  2. 遇到需要的功能再学习,小洁老师的patch包教程:大佬新包patchwork:可能是迄今为止最优秀的拼图包 (qq.com)
  • 画板被占用:dev.off()关闭画板,救不了就dev.new()开个新的~

如何搜索画图代码和资料

  1. 可以用bing/百度/搜狗微信搜索画图教程~
  2. STHDA:一个画图网站
  3. 小洁老师的画图合集: 画图合辑
  • 画图代码+数据+解决问题的能力 = 成品图

画图的正确思维

  1. 我的数据适合什么图展示?
  2. 搜画图代码
    • 代码可复制,图片美观
    • 提供了示例数据
  3. 仿制示例数据
    • 数据类型、数据结构
    • 组织方式、对应关系
  4. 套代码,调细节
  • 随机抽样:sample(向量,n),随机种子:set.seed(),每个随机种子对应一个抽样结果。

引用自生信技能树课程。给小洁老师比心~~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值