2014-04-01 124 views
5

我想在ggplot中创建一个2变量条形图,其中一个度量部分隐藏在另一个度量的后面。我可以使用Series Overlap在Excel中完成并获得this result在ggplot中调整geom_bar(position =“dodge”)

使用geom_bar(position =“dodge”)将两个条并排放置。有没有办法调整这一点?

一些代码:

library (ggplot2) 
library(reshape2) 
x <- c(19, 18, 21, 19) 
y <- c(17, 16, 18, 19) 
z <- c("a", "b", "c", "d") 

df <- melt (data.frame (x,y,z)) 

ggplot (df, aes(x=z, y=value, fill=variable)) + geom_bar (stat="identity", position ="dodge") 

回答

13

可以通过指定position = position_dodge(...)定制闪避。

ggplot (df, aes(x=z, y=value, fill=variable)) + 
    geom_bar (stat="identity", position = position_dodge(width = 0.5)) 

enter image description here

+0

我想指出的是定制的''position_dodge''量是超乎想象的。 http://ggplot2.tidyverse.org/reference/geom_bar.html – PatrickT