boxplots over bar graph

Hi, I'd like to:

  1. have one boxplot for each bar, with the mean (white square)
  2. stick the mean texts to the bottom

This is the reproducible of what I have so far. See the attached image with what this code does (top) and what I'd like to have instead (bottom). I don't need to have different colours for each group as in the bottom image.


boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
  geom_bar(position = "dodge", stat = "summary", fun.y = "mean") +
  geom_boxplot(outlier.shape = NA, width=0.1, color = "#8f8a86",fill="#8f8a86") +
  stat_summary(aes(label=round(..y..,2)), fun.y=mean, geom="text", size=8, color = "white", vjust=8, position = position_dodge(0.9)) +
  stat_summary(fun.y=mean, geom="point", shape=23, size=4, fill = "white") +
  labs(x = "Conditions", y = "Means") +
  scale_y_continuous(limits=c(0,7),oob = rescale_none) +

I think you will need to make a custom linear scale for your x axis (not as.factor(cyl)), to help you position the elements. Then write the xaxis ticks and labels manually.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Sorry I mean a continuous scale. You plot has a discrete x axis, which doesn't allow you to position things as you wish (factors are converted to 1,2,3,...).

Hi, thanks. could you provide an example or link me to some? not sure I understand the "linear scale" you mention..thanks!