简体   繁体   English

使用 ggplot 将误差线添加到 R 中的聚集条形图

[英]Adding error bars to a clustered bar graph in R with ggplot

I am trying to create a clustered bar graph with error bars for my independent variables (y axis) for each of my dependent variables (metrics on the x axis).我正在尝试为我的每个因变量(x 轴上的指标)创建一个带有误差条的聚类条形图,用于我的自变量(y 轴)。

My data:我的数据:

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))

Where SE1 is the standard error for each of my independent variables' data points for Species Richness (I haven't had the chance to add the other SE values for my other metrics, I'm just trying to experiment with the one for now).其中 SE1 是我的每个自变量的物种丰富度数据点的标准误差(我还没有机会为我的其他指标添加其他 SE 值,我现在只是想尝试一下) .

I've created a figure using this code:我使用此代码创建了一个图形:

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)

The barplot is not correct (SE1 is now one of the metrics along the x axis), and the error bars are not following my data for Species Richness.条形图不正确(SE1 现在是 x 轴上的指标之一),并且误差条不遵循我的物种丰富度数据。

Figure:数字:

数字

I'm unsure of what I'm doing wrong with the error bars and how to fix it!我不确定我在错误栏上做错了什么以及如何解决它!

You can remove the extra SE1 column by excluding it from melt:您可以通过从熔体中排除额外的 SE1 列来删除它:

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

Then for the error bar to appear in the Species Richness facet, you need to specify variable for it, with same levels as mm above:然后,为了让误差条出现在 Species Richness facet 中,您需要为其指定变量,其级别与上面的 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))

Now we plot:现在我们绘制:

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM