Making stacked bar plot with specified error bar values in R

前端 未结 1 356
旧巷少年郎
旧巷少年郎 2021-01-23 15:06

I\'m trying to make a stacked bar plot in R with error bars for a value that I want to predefine, rather than calculate, but each bar has a different value.

For example

1条回答
  •  深忆病人
    2021-01-23 15:56

    First, add the upper and lower bounds to your mx dataframe:

    library(dplyr)
    mx <- mx %>% group_by(Period) %>%
      mutate(pos = cumsum(value)) %>%
      ungroup() %>%
      mutate(ci = c(.5, .1, .2, .2),
             upper = pos + ci/2,
             lower = pos - ci/2)
    

    Then, add a geom_errorbar to your plot:

    ggplot(mx, aes(x=Period, y=value, fill=variable), xLabels=NA) +
      geom_bar(stat="identity") +
      geom_errorbar(aes(ymin = lower, ymax = upper), width = .2, col = "red") +
      facet_grid(~Sample) +
      scale_fill_manual(values = c("grey69","black")) +
      theme_bw() +
      xlab("") + 
      ylab ("")
    

    0 讨论(0)
提交回复
热议问题