2013-02-21 19 views
8

Tôi đang cố che phủ hai ô khác nhau. Một là geom_boxplot, số khác geom_jitter. Tôi muốn mỗi người có thang màu riêng. Nhưng khi tôi thêm thang màu thứ hai, tôi được đưa ra lỗiggplot2 - sử dụng hai thang màu khác nhau cho các ô được xếp chồng

"Scale for 'fill' is already present. Adding another scale for 'fill', 
    which will replace the existing scale." 

Tôi giả sử tôi đang làm điều gì sai. Lời khuyên nào sẽ đánh giá cao

Đây là một ví dụ thô mã làm việc của tôi:

P <- ggplot(dat) + 
      geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) + 
      scale_fill_manual(values=cpalette1) + 
      geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") + 
      theme(legend.position="none") 

P + geom_jitter(dat2, aes(x=ve, y=metValue, fill=atd), 
       size=2, shape=4, alpha = 0.4, 
       position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
       scale_fill_manual(values=cpalette2) 

datdat2 có giản đồ giống nhau, nhưng giá trị khác nhau.

Tôi đã tìm thấy một số ví dụ về cách xử lý đồ thị lớp phủ nhưng không có ví dụ nào xuất hiện để giải quyết mối quan tâm cụ thể này.

Trả lời

11

Đầu tiên, tạo hai khung dữ liệu mẫu có cùng tên như trong ví dụ.

dat<-data.frame(ve=rep(c("FF","GG"),times=50), 
       metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25), 
       atd=rep(c("HH","GG"),times=50)) 
dat2<-data.frame(ve=rep(c("FF","GG"),times=50), 
       metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25), 
       atd=rep(c("HH","GG"),times=50)) 

tôi cho rằng bạn không cần phải sử dụng lập luận fill= trong geom_jitter() vì màu cho shape=4 thể được thiết lập cũng với colour= tranh cãi. Sau đó, bạn có thể sử dụng scale_colour_manual() để đặt giá trị của mình. Thay vì cpallete, chỉ cần sử dụng tên màu.

P <- ggplot(dat) + 
    geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) + 
    geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") + 
    scale_fill_manual(values=c("red","blue","green","yellow"))+ 
    theme(legend.position="none") 

P + geom_jitter(data=dat2, aes(x=ve, y=metValue, colour=atd), 
       size=2, shape=4, alpha = 0.4, 
       position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
       scale_colour_manual(values=c("red","blue")) 

enter image description here