0

I want to create a violin plot with geom_jitter dots greater than a defined value.

Below is the picture and code I used to get this violin plot with a box plot and geom_jitter for data points. Is there a way to have geom_jitter for dots >58 and hide all the rest?

library(nycflights13)
library(ggplot2)
library(dplyr)

weather3 <- weather %>%
  filter(month == 3)

viol_plot <- ggplot(weather3, aes(x = factor(month), y = temp)) +
  geom_violin()

viol_plot + geom_boxplot(width=0.2) + geom_jitter(shape=20, position = position_jitter(0.05))

viol_plot + geom_jitter(shape=20, position = position_jitter(0.05))

violin plot picture

Thank you!

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Assir
  • 11
  • 2

1 Answers1

1

It is a just a matter of defining the data for the geom_jitter() separately from the original of ggplot definition.

weather3 <- weather %>%
   filter(month == 3)

viol_plot <- ggplot(weather3, aes(x = factor(month), y = temp)) +
   geom_violin()

viol_plot + geom_boxplot(width=0.2) + 
   geom_jitter(data=weather3[weather3$temp>58,], shape=20, position = position_jitter(0.05), color="blue")

I colored the jittered points blue for clarity.

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50