简体   繁体   中英

Adjusting geom_bar (position=“dodge”) in ggplot

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

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM