首页 > 解决方案 > gghighlight 创造加分

问题描述

当我突出显示时,gghighlight 似乎正在创造额外的分数。这里有什么提示吗?我想我一定做错了什么。

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)

编辑,添加情节:

第一个情节,正在工作

在此处输入图像描述

标签: rggplot2gghighlight

解决方案


似乎确实是这样:


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)

在此处输入图像描述 请参阅标有“3”的红色点左侧的 cyl=6 的灰色点。 在此处输入图像描述

#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)

在此处输入图像描述


#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)

在此处输入图像描述

现在有 9 个。总而言之:添加高亮似乎确实会绘制额外的点......


推荐阅读