I have been working with ggplot2 a lot over the past few weeks and was wondering if anyone could help me solve this problem I am having.
When I plot my boxplot my boxes are touching each other. I would like them to have a little bit of space between them. Is there a way to accomplish this? I am sure there is and I am just not seeing it.
Let's borrow the reproducible example provided by Kevin Ushey in this question:
set.seed(123)
dat <- data.frame(
x=rep( c(1, 2, 3, 4), times=25 ),
y=rnorm(100),
gp=rep(1:2, each=50)
)
p <- ggplot(dat, aes(x=factor(x), y=y))
p + geom_boxplot(aes(fill = factor(gp))) #example 1
Then, following the advice of Arun, I tested the (position = position_dodge(.))
but with geom_boxplot
instead of geom_bar
, and it worked.
In this case, it was not necessary to change the boxes width.
So, changing the last line of the above code to:
p + geom_boxplot(aes(fill = factor(gp)),position=position_dodge(1))
did the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With