Interval between boxes in ggplot2

I have worked a lot with ggplot2 over the past few weeks and wondered if anyone could help me solve this problem that I am having.

When I draw my box, my boxes touch each other. I would like them to have some space between them. Is there any way to do this? I am sure there is, and I just do not see it. enter image description here

+6
r ggplot2
source share
1 answer

Let me reproduce the reproducible example presented by Kevin Ears 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 

enter image description here

Then, following Arun’s advice, I tested (position = position_dodge(.)) , But with geom_boxplot instead of geom_bar , and it worked.

In this case, there is no need to change the width of the boxes.

So, changing the last line of the above code to:

 p + geom_boxplot(aes(fill = factor(gp)),position=position_dodge(1)) 

did the trick.

enter image description here

+16
source share

All Articles