R ggplot的geom_jitter如何处理重复的异常值

8

问:我正在使用ggplot的geom_boxplot绘制数据集。然而,当我尝试使用geom_jitter()绘制所有数据点时,我的数据中的离群值被复制了。其它所有数据点都正常。问题出在哪里?

示例代码:

PeakPeriod_24h <- c (31.05820, 23.83500, 24.36254, 25.31609, 24.21623, 23.90320) 
condition <- rep("HL",6)
data_HL <- data.frame(condition, PeakPeriod_24h)
p <- ggplot(data_HL, aes(x=condition, y=PeakPeriod_24h, fill=condition))
p + geom_boxplot()+
  geom_jitter(width = 0.3)+
  theme_bw()+
  coord_flip()+
  geom_hline(aes(yintercept=24.18), colour="brown1", linetype="dotted", size = 1.4)+
  scale_y_continuous(limits=c(), name = "Period Length")+
  ggtitle("Boxplots\nHabitual Light")+
  scale_fill_manual(values = c("gray60"))+
  theme(plot.title = element_text(size=14, face="bold", vjust = .5),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.title.x = element_text(size=12, face = "bold"),
    axis.text.x = element_text(size = 10, face = "bold", colour = "gray20"))+
  guides(fill=FALSE)

这里输入图片描述

谢谢!


当运行您的代码时,我收到了“错误:找不到对象'p'” 的提示。 - lukeA
抱歉,我忘记在代码中加入一行。现在已经添加了... - Christine Blume
1个回答

11

尝试

ggplot(data_HL, aes(x=condition, y=PeakPeriod_24h, fill=condition)) + 
  geom_boxplot(outlier.shape = NA) +  
  geom_jitter(width = 0.3) 

异常值被放入了两个图层中,一个是由geom_boxplot绘制的(除非你明确表示不想绘制异常值的点),另一个是由geom_jitter绘制的。

至于第二个问题,你可以使用

geom_jitter(width = 0.3, aes(color=I(c("black", "blue")[code+1L]))) 

我也要感谢你。我希望我能把我在Stack Overflow里看到的并通过Google搜索找到的东西节省下来的时间加起来! - jtolle
双倍 - 我也一样。 :) - lukeA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接