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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ggplot将误差线添加到R中的聚簇条形图中相关的知识,希望对你有一定的参考价值。

[我正在尝试为每个因变量(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轴的度量标准之一,并且误差线没有跟随我的“物种丰富度”数据。

图:

Figure

我不确定错误栏有什么问题以及如何解决!

答案

您可以通过从熔融中排除多余的SE1列来删除它:

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

然后错误栏出现在“物种丰富度”构面中,您需要为其指定变量,其级别与上面的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()

enter image description here

以上是关于使用ggplot将误差线添加到R中的聚簇条形图中的主要内容,如果未能解决你的问题,请参考以下文章

在 R 中的折线图中添加误差线(ggplot)

使用 D3.js 将误差线添加到分组条形图中

R语言plotly可视化:plotly可视化在对比条形图中添加误差条散点图中添加误差条线图中添加误差条(Error Bars with plotly in R)

R语言ggplot2可视化条形图(bar plot)并为条形图添加误差条(error bar)自定义设置误差条(error bar)的颜色/色彩( Barplots with Error bar)

R语言ggplot2可视化因子分组并排条形图柱状图可视化添加误差条:barplot with several variables side by side grouped by a factor

R语言plotly可视化:plotly可视化在对比条形图中添加误差条(Bar Chart with Error Bars with plotly in R)