13
votes

I want to create a 2 variable bar chart in ggplot where one measure is partially hidden behind the other. I can do it in Excel using Series Overlap and get this result.

Using the geom_bar (position="dodge") places the two bars side by side. Is there a way to adjust this at all?

Some code:

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")
2

2 Answers

23
votes

You can customise the dodging by specifying 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

1
votes

I solved it with the help of above, but further adjustment:

ggplot (df, aes(x=z, y=value, fill=variable)) + 
  geom_bar (stat="identity", position = position_dodge2(width = 0.5, preserve = "single", padding = -0.5))