垂直误差条对齐,堆积条图 ggplot

Vertically error bars allignment, stacked bars plot ggplot

我正在尝试获取一些带有误差线的堆叠条形图,但是对应于一组变量的误差线未对齐。

如果我做一个闪避姿势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)

你应该将下层堆栈的值加到上层堆栈的值

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)