TER
TER

Reputation: 119

One boxplot with Jitter and one without in the same viz

I would like to have a viz with one two boxplots, but only one of them have jittered data points. I can create the viz below, but want the 'non-jit" to not have the datapoints. Thanks for the help!

Amount = c(runif(20,1,100),5,25,50,75,90,(runif(20,1,100)),5,25,50,75,90)
Level = c(rep(1,25),rep(2,25))
Description = c(rep("jit",20),rep("non-jit",5),rep("jit",20),rep("non-jit",5))

Jitter = data.frame(Level,Description,Amount)
names(Jitter) = c("Level", "Description", "Amount")
View(Jitter)

Jitter$Description = factor(Jitter$Description, levels = c("jit", "non-jit"))
Jitter$Level = factor(Jitter$Level, levels = c("1", "2"))

ggplot(Jitter, aes(x=Amount, y = Description))+
  geom_boxplot()+
  geom_point(position = position_jitter())+
  facet_grid(Level~.)

boxplots

Upvotes: 1

Views: 163

Answers (1)

bouncyball
bouncyball

Reputation: 10781

If I understand you, we can just add tweak the data that is passed to geom_point. By default, geom_point inherits the aes as you've definied it in ggplot. By filtering the data, we just pass the points we wanted plotted

ggplot(Jitter, aes(x=Amount, y = Description))+
  geom_boxplot()+
  geom_point(data = Jitter %>% filter(Description != "non-jit"), 
             position = position_jitter())+
  facet_grid(Level~.)

enter image description here

Upvotes: 3

Related Questions