我有一些数据:
set.seed(100)
test_data = data.frame(ID = rep(1:100, 2),
status = rep(c(0,1,0,1), each = 50),
variable = rep(c("var1", "var2"), each = 100),
value = rnorm(n = 200))
我想将这些数据绘制成一个像这样的箱形图:
ggplot(test_data) +
geom_boxplot(aes(x = reorder(variable, value, FUN = median), y = value))
但我想根据状态列更改异常值的形状和/或颜色。因此,如果该点具有状态1,那么它是一个红色圆圈,如果它是状态0,那么它就是一个蓝点,例如。
我尝试过以下操作,但似乎没有正常工作。
for (i in names(table(test_data$variable))) {
for (j in c(0, 1)) {
iqr = quantile(test_data[test_data$variable %in% i & test_data$status %in% j,
"value"], c(0.25, 0.75))
whisker = iqr + c(-1.5, 1.5) * diff(iqr)
test_data[, "shape"] <- ifelse(test_data[test_data$variable %in% i &
test_data$status %in% j, "value"] < whisker[1]
|
test_data[test_data$variable %in% i &
test_data$status %in% j, "value"] > whisker[2],
1, 16)
}
}
test_data_shape = test_data[test_data$shape %in% 1, ]
test_data_shape$shape = ifelse(test_data_shape$status %in% 1, 15, 16)
ggplot(test_data) + geom_boxplot(aes(x = reorder(variable, value, FUN = median),
y = value)) +
geom_point(data = test_data_shape, aes(x = reorder(variable, value, FUN = median),
y = value, shape = factor(shape))) +
scale_shape_manual(guide=FALSE, values=c("16" = 16, "15" = 15))