如何更改点的参数(颜色、形状等)对应于geom_jitter中的异常值
有一个数据
> dput(head(df, 20))
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("W1",
"W3", "W4", "W5", "W12", "W13", "W14"), class = "factor"), value = c(68,
62, 174, 63, 72, 190, 73, 68, 62, 88, 81, 80, 79, 51, 73, 61,
NA, NA, 84, 87)), row.names = c(NA, 20L), class = "data.frame")和一个代码
plot <-
ggplot(df, aes(factor(df$variable), df$value)) +
geom_jitter(position = position_jitter(width = .1, height = 0), size = 0.7) +
theme(legend.position = 'none') +
theme_classic() +
labs(x = '',
y = '',
title = "test")我得到了这样的情节。

对于相同的数据,之前已经用默认的coef = 1.5创建了箱线图,所以我知道这个数据集中有异常值。现在,我只想创建点图,并将离群点着色为红色。对于geom_boxplot,这是通过单个函数参数outlier.color来完成的,但是geom_jitter没有这样的参数。
发布于 2019-12-30 01:26:16
您可以首先使用dplyr定义异常值
library(dplyr)
new_df <- df %>% group_by(variable) %>% filter(!is.na(value)) %>%
mutate(Outlier = ifelse(value > quantile(value, 0.75)+1.50*IQR(value),"Outlier","OK")) %>%
mutate(Outlier = ifelse(value < quantile(value, 0.25)-1.50*IQR(value),"Outlier",Outlier))
head(new_df)
# A tibble: 6 x 3
# Groups: variable [1]
variable value Outlier
<fct> <dbl> <chr>
1 W1 68 OK
2 W1 62 OK
3 W1 174 Outlier
4 W1 63 OK
5 W1 72 OK
6 W1 190 Outlier然后使用这个新列,您可以根据条件Outlier对数据集的子集进行批量处理
library(ggplot2)
ggplot(subset(new_df, Outlier == "OK"), aes(x = variable, y = value))+
geom_jitter(width = 0.1, size = 0.7)+
geom_jitter(inherit.aes = FALSE, data = subset(new_df, Outlier == "Outlier"),
aes(x = variable, y = value), width = 0.1, size = 3, color = "red")+
theme(legend.position = 'none') +
theme_classic() +
labs(x = '',
y = '',
title = "test")

https://stackoverflow.com/questions/59521033
复制相似问题