2011-11-15 38 views
8

Tôi đang cố gắng che phủ 2 thanh từ geom_bar bắt nguồn từ 2 dữ liệu riêng biệt. Khung hình.Làm cách nào để che phủ hai geom_bar?

dEQ 
    lab perc 
1 lmP 55.9 
2 lmN 21.8 
3 Nt 0.6 
4 expG 5.6 
5 expD 0.0 
6 prbN 11.2 
7 prbP 5.0 

LMD 
    lab perc 
1 lmP 16.8 
2 lmN 8.9 
3 Nt 0.0 
4 expG 0.0 
5 expD 0.0 
6 prbN 0.0 
7 prbP 0.0 

Cốt truyện đầu tiên là:

p <- ggplot(dEQ, aes(lab, perc)) + 
    xlab(xlabel) + ylab(ylabel) + 
    geom_bar(stat="identity", colour="blue", fill="darkblue") + 
    geom_text(aes(vecX, vecYEQ+1.5, label=vecYlbEQ), data=dEQ, size=8.5) + 
    theme_bw() + 
    opts(axis.text.x = theme_text(size = 20, face = "bold", colour = "black")) + 
    opts(axis.text.y = theme_text(size = 20, face = "bold", colour = "black")) + 
    coord_flip() + 
    scale_y_continuous(breaks=c(0,10,20,30,40,50,60), 
         labels=c("0","","20","","40","","60"), 
         limits = c(0, 64), expand = c(0,0)) 
print(p) 

nhưng tôi muốn overplot với geom_bar khác từ data.frame LMD

ggplot(LMD, aes(lab, perc)) + 
    geom_bar(stat="identity", colour="blue", fill="red", add=T) 

và tôi muốn để có một huyền thoại.

Trả lời

17

đây là một ví dụ:

p <- ggplot(NULL, aes(lab, perc)) + 
    geom_bar(aes(fill = "dEQ"), data = dEQ, alpha = 0.5) + 
    geom_bar(aes(fill = "LMD"), data = LMD, alpha = 0.5) 
p 

enter image description here

nhưng tôi khuyên bạn nên rbind họ và vẽ nó bằng cách né tránh:

dEQ$name <- "dEQ" 
LMD$name <- "LMD" 
d <- rbind(dEQ, LMD) 
p <- ggplot(d, aes(lab, perc, fill = name)) + geom_bar(position = "dodge") 

enter image description here

+0

Thank you very much – Tali

+0

thể bạn di chuyển dữ liệu đã được dodged bove nhau? giống như một khoảng cách tiêu cực giữa hai loại thanh? –

+0

@PeterPan Bạn có thể làm điều đó với 'position =" stack "'. – Reason

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