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

问题描述

我想通过指定美学属性来突出显示具有多个 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 调用之外进行计算?

enter image description here

解决方法

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))

step plot with points

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...