5

I'm trying to build a complex figure that overlays individual data points on a boxplot to display both summary statistics as well as dispersion of the raw data. I have 2 questions in rank order of importance:

  1. How do I center the jittered points around the middle of their respective box plot?
  2. How can I remove the dark dots from the "drv" legend?

Code:

library(ggplot2) library(dplyr) mpg$cyl <- as.factor(mpg$cyl) mpg %>% filter(fl=="p" | fl=="r" & cyl!="5") %>% sample_n(100) %>% ggplot(aes(cyl, hwy, fill=drv)) + stat_boxplot(geom = "errorbar", width=0.5, position = position_dodge(1)) + geom_boxplot(position = position_dodge(1), outlier.shape = NA)+ geom_point(aes(fill=drv, shape=fl), color="black", show.legend=TRUE, alpha=0.5, size=3, position = position_jitterdodge(dodge.width = 1)) + scale_shape_manual(values = c(21,23)) 

Example figure

1 Answer 1

7

It looks like the current dodging for geom_point is based on both fill and shape. Use group to indicate you only want to dodge on drv.

You can use override.aes in guide_legend to remove the points from the fill legend.

mpg %>% filter(fl=="p" | fl=="r" & cyl!="5") %>% sample_n(100) %>% ggplot(aes(cyl, hwy, fill=drv)) + stat_boxplot(geom = "errorbar", width=0.5, position = position_dodge(1)) + geom_boxplot(position = position_dodge(1), outlier.shape = NA)+ geom_point(aes(fill = drv, shape = fl, group = drv), color="black", alpha =0.5, size=3, position = position_jitterdodge(jitter.width = .1, dodge.width = 1)) + scale_shape_manual (values = c(21,23) ) + guides(fill = guide_legend(override.aes = list(shape = NA) ) ) 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.