垂直误差线对齐,堆积条形图ggplot
Posted
技术标签:
【中文标题】垂直误差线对齐,堆积条形图ggplot【英文标题】:Vertically error bars allignment, stacked bars plot ggplot 【发布时间】:2021-08-10 18:32:25 【问题描述】:我正在尝试获取一些带有误差线的堆积条形图,但是对应于一组变量的误差线没有对齐。
如果我做一个闪避位置position_dodge
(按照我在这里找到的一些其他问题的例子)那么它可以工作,但不幸的是这不是我需要的...... :(
我改变了“position_dodge()”的值, 我写过: geom_errorbar(aes(ymin=gp96-sd, ymax=gp96+sd, group= type96), 而且我之前已经定义了 ymin 和 ymax 的值,但没有任何帮助...
谢谢 Vk
这是我为闪避条图尝试的一个
geom_bar(position = position_dodge(),stat="identity") +
geom_errorbar(aes(ymin=gp96-sd, ymax=gp96+sd),
position=position_dodge(), stat="identity",width=0.7,size=0.01)
这是我为堆积条形图尝试的一个
ggplot(data=dfch97,aes(y=gp96,x=sample96,fill=type96))+
geom_bar(position = position_stack(),stat="identity") +
geom_errorbar(aes(ymin=gp96-sd, ymax=gp96+sd),
position=position_dodge(), stat="identity",width=0.7,size=0.01)
我的数据框
type96<- c("co2_96","NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96","NetCH4_96h")
gp96<- c( 13.066667,4.283333,11.783333,3.983333,12.616667,4.4,12.383333,4.3,12.783333,4.566667,12.466667,4.383333,11.533333,4.066667,12.816667,4.533333,12.92,4.56,12.516667,4.25,13.4,4.366667,12.45,4.316667,12.366667,4.233333)
sd<- c(2.1096603, 0.8232051, 1.553598, 0.7386925, 1.2448561, 0.6870226, 2.0311737, 0.8579044, 1.3585532, 0.7033254, 1.5933194, 0.7386925, 2.5303491, 1.1500725, 1.1373947, 0.5715476, 0.9066422, 0.5176872, 0.7026142, 0.3937004, 0.9570789, 0.6345602, 1.3003846, 0.6242329, 1.0875048, 0.3669696)```
dfch97 <- data.frame(type96, gp96, sd)
dfch97$type96=as.factor(dfch97$type96)
【问题讨论】:
无法重现您的示例:x=sample96 ... "sample96" 未定义。 【参考方案1】:您应该将下堆栈的值添加到上堆栈的值
library(ggplot2)
type96<- c("co2_96","NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96", "NetCH4_96h", "co2_96","NetCH4_96h")
gp96<- c( 13.066667,4.283333,11.783333,3.983333,12.616667,4.4,12.383333,4.3,12.783333,4.566667,12.466667,4.383333,11.533333,4.066667,12.816667,4.533333,12.92,4.56,12.516667,4.25,13.4,4.366667,12.45,4.316667,12.366667,4.233333)
sd<- c(2.1096603, 0.8232051, 1.553598, 0.7386925, 1.2448561, 0.6870226, 2.0311737, 0.8579044, 1.3585532, 0.7033254, 1.5933194, 0.7386925, 2.5303491, 1.1500725, 1.1373947, 0.5715476, 0.9066422, 0.5176872, 0.7026142, 0.3937004, 0.9570789, 0.6345602, 1.3003846, 0.6242329, 1.0875048, 0.3669696)
dfch97 <- data.frame(type96, gp96, sd)
dfch97$sample96 <- rep(1:13, each = 2)
dfch97$type96=as.factor(dfch97$type96)
for (i in 1:nrow(dfch97))
if(dfch97[i, 'type96'] == "co2_96")
dfch97[i,'u_conf'] <- dfch97[i,'gp96'] + dfch97[i+1, 'gp96'] + dfch97[i,'sd']
dfch97[i,'l_conf'] <- dfch97[i,'gp96'] + dfch97[i+1, 'gp96'] - dfch97[i,'sd']
else
dfch97[i,'u_conf'] <- dfch97[i,'gp96'] + dfch97[i,'sd']
dfch97[i,'l_conf'] <- dfch97[i,'gp96'] - dfch97[i,'sd']
ggplot(data=dfch97,aes(y=gp96,x=sample96,fill=type96))+
geom_bar(position = position_stack(),stat="identity") +
geom_errorbar(aes(ymin=l_conf, ymax=u_conf),
position=position_dodge(), stat="identity",width=0.7,size=0.01)
【讨论】:
以上是关于垂直误差线对齐,堆积条形图ggplot的主要内容,如果未能解决你的问题,请参考以下文章