Adding error bars to a barchart with multiple groups

前端 未结 2 1168
别跟我提以往
别跟我提以往 2021-01-05 15:04

I have the following barchart to which I want to add error bars.

library(lattice)    
barchart(Change~fTreat,groups=Process,change,
          auto.key=l         


        
2条回答
  •  醉酒成梦
    2021-01-05 15:43

    This is not what you're asking for, but the plot is rather easy to make with ggplot2 (in a case that this is an option)

    dt <- structure(list(Treat = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L), .Label = c("12-380", "12-750", "8-380", "8-750"), class = "factor"), 
        Process = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Resp", 
        "Cal"), class = c("ordered", "factor")), Change = c(-33.05, 
        -34.74, 20.94, 18.06, 6.85, -28.57, -8.1, -78.72), upper = c(-13.22896628, 
        -28.61149669, 31.29930461, 27.30173776, 39.73271282, 9.458372948, 
        13.11035572, -47.03745704), lower = c(-52.86120694, -40.87446411, 
        10.57421563, 8.822042178, -26.03144161, -66.60447035, -29.30563327, 
        -110.3973761), fTreat = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
        3L, 4L), .Label = c("8-380", "8-750", "12-380", "12-750"), class = c("ordered", 
        "factor"))), .Names = c("Treat", "Process", "Change", "upper", 
    "lower", "fTreat"), row.names = c(NA, -8L), class = "data.frame")
    
    a <- ggplot(dt, aes(y = Change, x = Treat, ymax = upper, ymin = lower))
    dodge <- position_dodge(width=0.9)
    a + geom_bar(aes(fill = Process), position = dodge) +
    geom_errorbar(aes(fill = Process), position = dodge, width = 0.2)
    

    enter image description here

提交回复
热议问题