I would like to highlight some points on a ggplot
with multiple ECDFs by specifying an aesthetic attribute.
I tried the following:
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))
However, as you can see from the plot, all the points are misaligned, maybe because the grouping by col = Species
was not taken into account.
Is it possible to obtain the desired results, avoiding calculations outside the ggplot
call?
It doesn't appear that the geoms included with ggplot2 will do this. You could write your own geom if you like, but the easier way is just to do the data manipulation yourself. ggplot works best when you let it do the plotting, not trying to make it do all the data summarization
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))