mrsteve
mrsteve

Reputation: 4142

ggplot2: provide a min size for a box in a boxplot (so that not

I have the following boxplot:

df1 <- data.frame(f = c("a","b","c","c","c"), x=c(1,1,1,5,9))
qplot(factor(f),as.numeric(x),data=df1) + geom_boxplot()

For two factors the box of the boxplot, the "box" is just a line. Is it possible to increase the size of the box of those two factors, so that a microscopic white box is shown? Or that I can provide manually a default minimum width?

Upvotes: 0

Views: 587

Answers (1)

colcarroll
colcarroll

Reputation: 3682

You probably shouldn't do that, as it is at least a little dishonest. If you're really set on it, though, here's a bit of a hack that might work:

ggplot(df1, aes(x = factor(f), y = as.numeric(x))) +
  geom_boxplot(size = 2) + 
  geom_boxplot(size = 1, color = "white")

If you want to retain the look of the honest boxplots, you could do something like

library('plyr')
flat <- subset(ddply(df1, .(f), summarise, flat = length(unique(x))), flat == 1)$f
flat <- df1[df1$f %in% flat,]

ggplot(df1, aes(x = factor(f), y = as.numeric(x))) +
  geom_boxplot()
  geom_boxplot(data = flat, size = 2) + 
  geom_boxplot(data = flat, size = 1, color = "white")

Upvotes: 1

Related Questions