1

I am trying to get some stacked bar plots, with the error bars, however the error bars corresponding to one group of variables are not aligned.

If I do a dodge position position_dodge (following the examples of some other questions that I found here) then it works, but unfortunately it is not what I need.... :(

I have changed the values of the "position_dodge()", I have wrote: geom_errorbar(aes(ymin=gp96-sd, ymax=gp96+sd, group= type96), and also I have previously defined the values of ymin and ymax but nothing helps me...

Thanks Vk

This one is the one that I tried for the dodge bar plot

  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)

enter image description here

This one is the one that I tried for the stacked bar plot

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)



error bars of the pink bars are not aligned

My dataframe

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)

1
  • It's not possible to reproduce your example: x=sample96 ... "sample96" is not defined. Commented May 21, 2021 at 14:37

1 Answer 1

1

You should add value of lower stack to the value of upper stack

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)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.