Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Move jittered points to align with dodged boxplots but the points have different shapes

Tons of answers for dodging points and boxplots at the same time but what if I want different shaped points by a 3rd factor?

    data<-cbind(expand.grid(trt=factor(1:4), block=factor(1:2), year=factor(c(2013, 2022))), value=runif(512))

I have:

      ggplot(data, aes(x = trt, y = value, color = year)) +
      geom_boxplot(outlier.size = 0,size=3,alpha=0.7) +
      geom_point(alpha=0.4, position = position_jitterdodge(dodge.width = 1),
                 size=3,aes(shape=factor(block)))

And

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      geom_boxplot(position = position_dodge2(0.85, preserve = "single"), outlier.size=0, size=3,alpha=0.7) + 
        geom_jitter(position = position_dodge2(0.85,preserve = "single"),aes(shape=factor(block)),size=3,alpha=0.4)

Both of which produce roughly the same thing:
Note the points aren't over their respective colored boxplots

How do I make this, but the points from each "year" are displayed dodged to the same side that their respective year is dodged in the boxplots? As it stands, ggplot is putting all of block 1 with 2013 every time instead of just those block 1 points that actually also are in year==2013.

>Solution :

You can use the group aesthetic

ggplot(data, aes(x = trt, y = value, color = year)) +
    geom_boxplot(outlier.size = 0, alpha = 0.7, size = 2) +
    geom_point(alpha = 0.4, position = position_jitterdodge(jitter.width = 0.8),
               size = 3, aes(shape = factor(block), group = year))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading