rggplot2gghighlight

gghighlight creating extra points


gghighlight seems to be creating extra points when I highlight. Any tips here? I imagine I must be doing something wrong.

library(tidyverse)
library(gghighlight)
mtcars<-mtcars

#produces as expected
ggplot(mtcars) + 
  geom_jitter(aes(x=factor(cyl),y=wt,col=factor(gear)),show.legend = FALSE)
#produces as expected

#creates extra points?
ggplot(mtcars) + 
  geom_jitter(aes(x=factor(cyl),y=wt,col=factor(gear)),show.legend = FALSE)+gghighlight::gghighlight(wt>3,label_key = gear)

Edit, adding plots:

First plot, which is working

enter image description here


Solution

  • Does indeed seem to be the case:

    
    library(tidyverse)
    library(gghighlight)
    
    mtcars <- mtcars
    
    #using geom_point insted of geom_jitter since geom_jitter adds noise to the points, thus making plots tougher to reproduce and compare:
    
    #no highlight:
    ggplot()+
      geom_point(mtcars, mapping = aes(x = factor(cyl),y = wt,col = factor(gear)), show.legend = FALSE,, position=position_dodge(width = 0.5))
    
    #with highlight:
    ggplot()+
      geom_point(mtcars, mapping = aes(x = factor(cyl),y = wt,col = factor(gear)), show.legend = FALSE, position=position_dodge(width = 0.5))+
      gghighlight(wt > 3,label_key = gear)
    

    enter image description here see grey points for cyl=6 to the left of the red points labelled with a "3". enter image description here

    #new points seem to appear for cyl=6 (grey ones to the left), so lets look at these specifically:
    #first: how many values should be there?
    only_cyl_6 <- subset(mtcars, mtcars$cyl==6)
    length(only_cyl_6$wt)
    #7 
    #but we see 6 points in our first plot. A quick look @ only_cyl_6 reveals that #two values have the same wt value:
    only_cyl_6
    # -> Merc 280 & Merc 280C      
    #so the first plot seems to be fine...geom_jitter supports this.
    
    ggplot()+
      geom_jitter(only_cyl_6, mapping = aes(x = factor(cyl),y = wt,col = factor(gear)), show.legend = FALSE)
    
    

    enter image description here

    
    #now we see 7 point. thats good.
    
    #lets add highlight again:
    
    ggplot()+
      geom_jitter(only_cyl_6, mapping = aes(x = factor(cyl),y = wt,col = factor(gear)), show.legend = FALSE)+
      gghighlight(wt > 3,label_key = gear)
    
    

    enter image description here

    Now there are 9. So in conclusion: adding highlight does indeed seem to make additional points being plotted...