Niels
Niels

Reputation: 151

How to increase the distance between boxplots (ggplot2) which have discrete x-axis - position_dodge?

I am trying to change the distance between boxplots in a ggplot2 figure. I have boxplots which show the data for different categories of products. The categories (x-axis) are discrete variables. I tried position_dodge which did not affect the distance between the boxplots. It worked when the x-axis was a continuous variable.

Which command would increase the distance between the boxplots when x-axis is discrete?

require(ggplot2)
dat<-rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)),
  data.frame(approach=1,product=2,value=seq(5,15,0.3)),
  data.frame(approach=1,product=3,value=seq(5,17,0.2)),
  data.frame(approach=2,product=1,value=seq(1,13,0.3)),
  data.frame(approach=2,product=2,value=seq(3,18,0.5)),
  data.frame(approach=2,product=3,value=seq(4,25,0.7)),
  data.frame(approach=3,product=1,value=seq(1,15,0.6)),
  data.frame(approach=3,product=2,value=seq(3,16,0.5)),
  data.frame(approach=3,product=3,value=seq(1,10,0.1)))
dat$product<-as.factor(dat$product)

gg1<-ggplot(dat,aes(x =product, y = value, width = 4)) +
     geom_boxplot(position=position_dodge(1))
gg1

enter image description here

Upvotes: 1

Views: 422

Answers (1)

Matias Andina
Matias Andina

Reputation: 4220

Not entirely clear but...Do you want to play with width ?

geom_boxplot(width=0.5) produces

enter image description here

geom_boxplot(width=0.1) produces

enter image description here

Upvotes: 1

Related Questions