无需外部计算即可在 ECDF 上加分

Adding points on ECDF without external calculations

我想通过指定美学属性来强调 ggplot 具有多个 ECDF 的一些要点。

我尝试了以下方法:

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 中包含的 geom 似乎不会执行此操作。如果愿意,您可以编写自己的 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))