In ggplot2, I want my boxes in a box plot to be of equal width even when a given level combination does not exist.
For example, in mtcars, cyl=8 and gear=4 does not exist, which leads to larger bars in this plot:
qplot(data=mtcars, x=as.factor(cyl), y=mpg,
colour=as.factor(gear), geom="boxplot")
For a bar plot, padding our data frame with NA values for these level combinations would solve the problem, but not for box plot:
mtcars.fill <- data.frame(cyl=8,gear=4,mpg=NA)
mtcars <- rbind.fill(mtcars,mtcars.fill)
qplot(data=mtcars, x=as.factor(cyl), y=mpg, colour=as.factor(gear), geom="boxplot")
Warning message:
Removed 1 rows containing non-finite values (stat_boxplot).
Which leads to the exact same plot.
stat_boxplot has an argument for na values, but it is set to not remove NAs by default:
na.rm = FALSE