R语言之可视化①误差棒

作者: 柳叶刀与小鼠标 | 来源:发表于2018-11-01 01:03 被阅读51次

    本教程介绍如何使用R软件和ggplot2包创建带有误差棒的图形。 可以使用以下函数创建不同类型的错误栏:

    geom_errorbar()
    geom_linerange()
    geom_pointrange()
    geom_crossbar()
    geom_errorbarh()

    准备数据
    使用ToothGrowth数据。 它描述了维生素C对豚鼠牙齿生长的影响。 使用三种剂量水平的维生素C(0.5mg,1mg和2 mg)和两种递送方法[橙汁(OJ)或抗坏血酸(VC)]中的每一种:

    > library(ggplot2)
    > df <- ToothGrowth
    > df$dose <- as.factor(df$dose)
    > head(df)
       len supp dose
    1  4.2   VC  0.5
    2 11.5   VC  0.5
    3  7.3   VC  0.5
    4  5.8   VC  0.5
    5  6.4   VC  0.5
    6 10.0   VC  0.5
    

    len:齿长
    dose:剂量单位为mg(0.5,1,2)
    supp:补充类型(VC或OJ)
    在下面的示例中,我们将绘制每组中Tooth长度的平均值。 标准差用于绘制图形上的误差线。首先,使用下面的辅助函数将用于计算每组感兴趣变量的均值和标准差。

    #+++++++++++++++++++++++++
    # Function to calculate the mean and the standard deviation
    # for each group
    #+++++++++++++++++++++++++
    # data : a data frame
    # varname : the name of a column containing the variable
    #to be summariezed
    # groupnames : vector of column names to be used as
    # grouping variables
    data_summary <- function(data, varname, groupnames){
      require(plyr)
      summary_func <- function(x, col){
        c(mean = mean(x[[col]], na.rm=TRUE),
          sd = sd(x[[col]], na.rm=TRUE))
      }
      data_sum<-ddply(data, groupnames, .fun=summary_func,
                      varname)
      data_sum <- rename(data_sum, c("mean" = varname))
      return(data_sum)
    }
    df2 <- data_summary(ToothGrowth, varname="len", 
                        groupnames=c("supp", "dose"))
    # Convert dose to a factor variable
    df2$dose=as.factor(df2$dose)
    head(df2)
    
    • 向条形图添加误差线

    函数geom_errorbar()可用于生成误差棒:

    library(ggplot2)
    # Default bar plot
    p<- ggplot(df2, aes(x=dose, y=len, fill=supp)) + 
      geom_bar(stat="identity", color="black", 
               position=position_dodge()) +
      geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
                    position=position_dodge(.9)) 
    print(p)
    # Finished bar plot
    p+labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+
      theme_classic() +
      scale_fill_manual(values=c('#999999','#E69F00'))
    
    • 使用线图绘制误差棒
    
    # Default line plot
    p<- ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) + 
      geom_line() +
      geom_point()+
      geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
                    position=position_dodge(0.05))
    print(p)
    # Finished line plot
    p+labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+
      theme_classic() +
      scale_color_manual(values=c('#999999','#E69F00'))
    

    • 使用点图绘制误差棒
    p <- ggplot(df, aes(x=dose, y=len)) + 
      geom_dotplot(binaxis='y', stackdir='center')
    # use geom_crossbar()
    p + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1), 
                     geom="crossbar", width=0.5)
    # Use geom_errorbar()
    p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), 
                     geom="errorbar", color="red", width=0.2) +
      stat_summary(fun.y=mean, geom="point", color="red")
    
    # Use geom_pointrange()
    p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), 
                     geom="pointrange", color="red")
    

    使用函数geom_dotplot()和stat_summary():

    平均值+/- SD可以添加为误差条或点范围:


    相关文章

      网友评论

      • Yang_1817:可以在比较组上面加显著性标记吗
        柳叶刀与小鼠标:@Yang_1817 我记得ggpubr这个包可以,就是效果不好看,我不画难看的图,所以就没用那个包
        Yang_1817:@夜神moon 我用其他包辅助添加了显著性标记,只是有点麻烦
        柳叶刀与小鼠标:@Yang_1817 加显著性标记这个最好用ps,简单美观

      本文标题:R语言之可视化①误差棒

      本文链接:https://www.haomeiwen.com/subject/eaeatqtx.html