Reputation: 135
I'm making a faceted boxpot with ggplot2. I want to evenly increase the vertical spaces between the boxplots in each facet so I can include some text in those spaces, but I haven't been able to do it so far.
I used the function position_dodge()
and increased the width, as suggested here: ggplot increase distance between boxplots, but the plot remains the same without any changes. Here is a piece of code that allows you to reproduce the problem:
library(ggplot2)
set.seed(2)
bp_data <- data.frame(Result=runif(100, min=0, max=2),
Method=rep(c("s1", "s2", "s3", "s4"), 25),
Var=rep(c("v1", "v2", "v3", "v4", "v5"), 20),
stringsAsFactors=FALSE)
bp <- ggplot(bp_data) +
aes(x = Method, y = Result) +
geom_boxplot(width=0.7, position=position_dodge(width=5.0)) +
coord_flip() +
facet_grid(Var ~ .)
bp
Setting different values for the width
parameter of function position_dodge
doesn't have any effect.
Please, note that what I want to do is to increase the space between the boxplots inside each facet and not to increase the space between facets.
Upvotes: 3
Views: 419
Reputation: 7724
One work-around is converting your Method into a numeric variable and then increase the numeric values:
bp_data$Method_num <- as.integer(factor(bp_data$Method))
bp_data$Method_num <- 1.5 * bp_data$Method_num
bp <- ggplot(bp_data) +
aes(x = Method_num, y = Result, group = Method) +
geom_boxplot(width=0.7, position=position_dodge(width=5.0)) +
coord_flip() +
facet_grid(Var ~ .) +
scale_x_continuous(breaks = unique(bp_data$Method_num),
labels = unique(bp_data$Method)) +
theme(panel.grid.minor.y = element_blank())
bp
Upvotes: 2