如何叠加两个geom_bar? [英] How to overlay two geom_bar?

查看:167
本文介绍了如何叠加两个geom_bar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖2个来自2个独立data.frames的 geom_bar 的条。

I'm trying to overlay 2 the bars from geom_bar derived from 2 separate data.frames.

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

第一个图是:

The first plot is:

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)

但是我想用data.frame中的另一个 geom_bar 来重叠绘图 LMD

but I want to overplot with another geom_bar from data.frame LMD

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

,我想要一个图例。

推荐答案

这里是一个例子:

here is an example:

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

,但我建议 rbind 它们,并用dodgi ng:

but I recommend to rbind them and plot it by dodging:

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

这篇关于如何叠加两个geom_bar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆