【问题标题】:Adding error bars to a clustered bar graph in R with ggplot使用ggplot将误差线添加到R中的聚集条形图中
【发布时间】:2020-01-13 22:21:26
【问题描述】:

我正在尝试为我的每个因变量(x 轴上的指标)创建一个带有自变量(y 轴)误差条的聚集条形图。

我的数据:

df <- data.frame (Parameter_Estimate=c('Burnham','Calumet','Northerly','Orland','Hickory Hammock','Lake O','MacArthur','Site E','Corrales','Oxbow','Tingley','Galilee','Jacobs Point','Prudence Island','Town Pond','Trial 1','Trial 2','2017','2018','Spring','Summer','0.5m','1m','Chicago','Florida','New Mexico','Rhode Island'),
     Species.Richness=c(79.4, -20.6, -12.6, -4, -63.15, -66.4, -69.15, -70.65, -52.07, -36.07, -67.23, -67.98, -69.9, -70.53, -74.73, 30.877, -2.743, 29.346, 0.9053, 29.721, -0.266, 28.898, 2.697, 70.1, -58.04, -42.49, -61.51),
     FQI=c(29.272,-0.271,-1.095,-2.652,-20.24,-19.66,-21.2,-22.56,-10.36,-5.158,-18.66,-8.802,-8.431,-7.372,-15.25,18.474,-0.14,17.869,2.1378,17.809,0.9189,18.155,1.004,28.268,-19.91,-10.39,-9.196),
     MeanC=c(3.294,0.4964,0.2252,-0.212,-0.631,-0.299,-0.339,-0.758,0.3185,0.3831,-0.135,2.7739,3.5218,4.089,3.1736,4.0308,0.1061,4.003,0.3103,4.0638,0.0266,4.0943,-0.052,3.4215,-0.634,0.0615,3.1985),
     Per.Non.Nat=c(11.912,-3.756,7.666,10.192,14.888,71.874,-4.33,-3.784,-6.288,9.517,3.31,-10.91,-11.65,-11.88,-6.388,16.086,-1.423,14.33,4.372,12.659,4.25,15.582,-0.702,15.437,16.136,-1.346,13.58),
     Shannon= c(3.6465,-0.449,-0.216,-0.4,-2.219,-2.897,-2.712,-2.504,-1.507,-0.798,-2.428,-2.066,-2.233,-2.528,-3.289,1.9313,-0.052,1.8939,0.268,1.8433,0.0975,1.8852,0.086,3.3804,-2.317,-1.311,-2.263),
     Weight.MeanC=c(3.3643,1.0304,0.4379,-0.428,-1.906,-2.679,-1.352,-1.106,1.8142,0.7625,0.1685,4.6151,5.1463,4.3978,3.6472,4.3178,0.0385,4.4046,-0.273,4.3543,-0.028,4.3357,-0.011,3.6243,-2.021,0.6551,4.1965),
     SE1=c(2.697,3.814,3.814,3.814,4.045,4.045,4.045,4.045,3.21,3.21,3.21,3.21,3.21,3.438,3.21,12.194,1.89,12.185,2.2041,12.299,2.0314,12.394,2.271,2.249,3.374,2.805,2.713))

SE1 是我的每个自变量的物种丰富度数据点的标准误差(我没有机会为我的其他指标添加其他 SE 值,我只是想尝试一下暂时)。

我使用此代码创建了一个图形:

mm <- melt (df, id.vars='Parameter_Estimate')

ggplot (mm, aes (x=Parameter_Estimate, y=value) + geom_bar (stat='identity') + coord_flip()+facet_grid (.~variable) + labs (x=',y=')+scale_x_discrete (limits=df$Parameter_Estimate) +
  geom_errorbarh (data=df, aes (y=Species.Richness, xmin=SE1, xmax=SE1), size=5, color="blue",  inherit.aes = FALSE)

条形图不正确(SE1 现在是沿 x 轴的指标之一),并且误差条没有跟随我的物种丰富度数据。

图:

我不确定我在错误栏上做错了什么以及如何解决它!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以将多余的 SE1 列从熔体中排除:

    mm <- melt (df[,-ncol(df)], id.vars='Parameter_Estimate')
    

    那么为了让错误栏出现在 Species Richness 方面,您需要为其指定变量,其级别与上面的 mm 相同:

    se_df = df[,c("Parameter_Estimate","Species.Richness","SE1")]
    se_df$variable="Species.Richness"
    se_df$variable=factor(se_df$variable,levels=levels(mm$variable))
    

    现在我们开始绘制:

    ggplot (mm, aes (x=Parameter_Estimate, y=value)) + 
    geom_bar (stat='identity') + 
    facet_grid (~variable) + labs (x='',y='')+
    geom_errorbar(data=se_df, inherit.aes=FALSE,aes (x=Parameter_Estimate,
    ymin=Species.Richness-SE1, ymax=Species.Richness+SE1),
    size=1, color="blue")+
    scale_x_discrete(limits=df$Parameter_Estimate)+
    coord_flip()
    

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      相关资源
      最近更新 更多