2014-04-01 125 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