Michael Bird
Michael Bird

Reputation: 783

Adding multiple points to a ggplot ecdf plot

I'm trying to generate a ggplot only C.D.F. plot for some of my data. I am also looking to be able to plot an arbitrary number of percentiles as points on top. I have a solution that works for adding a single point to my curve but fails for multiple values.

This works for plotting one percentile value

TestDf <- as.data.frame(rnorm(1000))
names(TestDf) <- c("Values")
percentiles <- c(0.5)

ggplot(data = TestDf, aes(x = Values)) +
  stat_ecdf() +
  geom_point(aes(x = quantile(TestDf$Values, percentiles),
                 y = percentiles))

Here is the output

However this fails

TestDf <- as.data.frame(rnorm(1000))
names(TestDf) <- c("Values")
percentiles <- c(0.25,0.5,0.75)

ggplot(data = TestDf, aes(x = Values)) +
  stat_ecdf() +
  geom_point(aes(x = quantile(TestDf$Values, percentiles),
                 y = percentiles))

With error

Error: Aesthetics must be either length 1 or the same as the data (1000): x, y

How can I add an arbitrary number of points to a stat_ecdf() plot?

Upvotes: 1

Views: 2210

Answers (1)

M--
M--

Reputation: 28825

You need to define a new dataset, outside of the aesthetics. aes refers to the original dataframe that you used for making the CDF (in the original ggplot argument).

ggplot(data = TestDf, aes(x = Values)) +
  stat_ecdf() +
  geom_point(data = data.frame(x=quantile(TestDf$Values, percentiles),
              y=percentiles), aes(x=x, y=y))

enter image description here

Upvotes: 2

Related Questions