问题描述
我想通过指定美学属性来突出显示具有多个 ECDF 的 ggplot
上的一些点。
我尝试了以下方法:
iris$dot <- ifelse(iris$Sepal.Length < 6,"<",">")
ggplot(iris,aes(x = Sepal.Length,col = Species)) +
stat_ecdf() +
geom_point(aes(y = ecdf(Sepal.Length)(Sepal.Length),#stat_ecdf doesn't seem to support shape aes
shape = dot)) +
scale_shape_manual(values = c(3,NA))
但是,从图中可以看出,所有点都未对齐,可能是因为未考虑按 col = Species
进行分组。
是否有可能获得所需的结果,避免在 ggplot
调用之外进行计算?
解决方法
ggplot2 中包含的 geoms 似乎不会这样做。如果愿意,您可以编写自己的 geom,但更简单的方法是自己进行数据操作。 ggplot 当你让它做绘图时效果最好,而不是试图让它做所有的数据汇总
iris %>%
group_by(Species) %>%
mutate(y = ecdf(Sepal.Length)(Sepal.Length)) %>%
ggplot(aes(Sepal.Length,y,color=Species)) +
geom_step() +
geom_point(aes(shape=dot)) +
scale_shape_manual(values = c(3,NA))