2012-04-30 28 views
6

Tôi có thanh barchart sau đây mà tôi muốn thêm các thanh lỗi.Thêm các thanh lỗi vào một barchart với nhiều nhóm

library(lattice)  
barchart(Change~fTreat,groups=Process,change, 
      auto.key=list(points=FALSE,rectangles=TRUE), 
      panel=function(x, y,...){ 
      panel.barchart(x,y,origin = 0,...); 
      panel.abline(h=0,col="black",...); 
      } 
     ) 

enter image description here

Tôi đã thử bằng cách sử dụng panel.errbars từ gói memisc mà hoạt động tuyệt vời cho xyplots, nhưng khi tôi thêm nó vào mã của tôi nó không tôn trọng các nhóm.

library(memisc)  
barchart(cbind(Change,lower,upper)~fTreat,groups=Process,change, 
    ylab="Pocertage change", 
    ylim=-115:50, 
    scales=list(alternating=FALSE, 
       tick.number=7, 
       tck=c(-1,0)), 
    panel=function(x, y,groups,...){ 
     panel.barchart(x,y=change$Change,groups=change$Process,origin = 0,...); 
     panel.abline(h=0,col="black",...); 
     panel.errbars(x,y,make.grid="none",ewidth=0.2,type="n",...) 
    } 
    ) 

enter image description here Bất kỳ ý tưởng làm thế nào để thêm các thanh lỗi để âm mưu của tôi, hoặc bằng cách sử dụng panel.errbars hay bất kỳ chức năng khác?

Dữ liệu:

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") 

Cheers

Trả lời

4

Dưới đây là một khác Tôi đã được đưa ra bằng cách sử dụng lưới.

prepanel=function(y, stderr, subscripts=subscripts, ...){ 
    uy <- as.numeric(y+stderr[subscripts]) 
    ly <- as.numeric(y-stderr[subscripts]) 
    list(ylim=range(y,uy,ly, finite=TRUE)) 
} 
panel.err=function(x, y, subscripts, groups, stderr, box.ratio, ...){ 
    d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio) 
    g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d 
    panel.arrows(as.numeric(x)+g,y-stderr[subscripts], as.numeric(x)+g, y+stderr[subscripts], 
       code=3,angle=90, length=0.025) 
} 
barchart(Change~fTreat,groups=Process,change, 
     stderr=change$stderr, 
     ylab="Pocertage change", 
     xlab="Treatment", 
     ylim=-115:50, 
     auto.key=list(points=FALSE,rectangles=TRUE,columns=2), 
     scales=list(alternating=FALSE, 
        tick.number=7, 
        tck=c(-1,0)), 
     prepanel=prepanel, 
     panel=function(x, y, subscripts, groups, stderr, box.ratio, ...){ 
      panel.barchart(x, y, subscripts=subscripts, 
          groups=groups, box.ratio=box.ratio,origin=0, ...) 
      panel.abline(h=0,col="black",...) 
      panel.err(x, y, subscripts=subscripts, 
         groups=groups, box.ratio=box.ratio,stderr=change$stderr) 
      } 
     ) 

cảm ơn Một lớn bạn Walmes Marques Zeviani cho việc cung cấp mã

Đây là dữ liệu sửa đổi:

change <- 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("Respiration", 
    "Calcification"), class = c("ordered", "factor")), Change = c(-33L, 
    -35L, 21L, 18L, 7L, -29L, -8L, -79L), stderr = c(20L, 6L, 
    10L, 9L, 33L, 38L, 21L, 32L), 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", "stderr", "fTreat"), row.names = c(NA, -8L 
), class = "data.frame") 
3

Đây không phải là những gì bạn đang yêu cầu, nhưng cốt truyện là khá dễ dàng để làm với ggplot2 (trong một trường hợp rằng đây là một lựa chọn)

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

+0

cảm ơn, tôi sẽ cung cấp cho nó một đi, rất thích tìm một giải pháp trong mạng lưới, đó là nếu có ai có – BDM

Các vấn đề liên quan