0

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

EconMatt
  • 339
  • 2
  • 7
  • Both codes work fine, what do you mean by extra points? – Duck Dec 16 '20 at 14:11
  • @Duck It seems like new point appear with the highlight. For instance: theres 3 points w/ wt>5 and cyl==8 (visible in the first line). Once the highlight is added, there seems to be new unhighlighted points around those original 3 points. – EconMatt Dec 16 '20 at 15:05

1 Answers1

2

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...

Chris
  • 141
  • 3
  • thanks for the help on this. Your example works well except I seem to see 9 points. I'm starting to think it's creating new points to highlight and the jitter leaves the old points visible (normally would be covered). – EconMatt Dec 16 '20 at 15:16
  • Your absolutely right. There are indeed 9 points with highlight. Seems like I cant count. Edited my Answer accordingly and added pictures illustrating the issue. Sorry. – Chris Dec 16 '20 at 16:08