How to customize whisker lines on a geom_box plot

2019-07-11 04:59发布

问题:

Because of a demanding end user, I need to learn whether the whisker lines on a geom_box plot can be colored or typed differently than the box itself?

Having just considered boxplot with colored and dotted lines, I have created a minimal example.

year <- rep("2014", 10)
total <- c(seq(55, 90, 5), 100, 40)
df <- data.frame(year = as.factor(year), total = total)

ggplot(df, aes(x=factor(year), y=total)) + 
  geom_boxplot(linetype = "dotted", color = "red") +
  theme_bw()

Can the plot below have green whiskers, keeping the red box, or solid whiskers keeping the dotted box?

This SO question tells us that base R permits whisker line customization aplenty. bxp has several parameters

EDIT after comment: I didn't spot the SO question user20650 graciously pointed out. Here is its answer -- plot boxplots twice.

ggplot(df, aes(x=factor(year), y=total)) + 
  geom_boxplot(linetype = "dotted", color = "red") +
  geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) + 
  theme_bw()

回答1:

EDIT after comment: I didn't spot the SO question user20650 graciously pointed out. Here is its answer -- plot boxplots twice.

ggplot(df, aes(x=factor(year), y=total)) + 
  geom_boxplot(linetype = "dotted", color = "red") +
  geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) + 
  theme_bw()