stacksia
stacksia

Reputation: 661

ggplot's stat_ecdf adds two extra points

In ggplot2, when I try to plot cumulative density function using stat_ecdf and with geom_point, I see a strange behavior: two extra points are added to my numbers, one before everything, and another one after everything else. This kind of make sense if the default geom_step was used for plotting, but with geom_point, this is very confusing if not outright wrong. Does anyone know how to fix this?

Here is an example:

qplot(a,data=data.frame(a=1:10),stat="ecdf",geom="point")

which produces:

ecdf plot with one point extra point at zero and one at one

Note the extra points at 0 and at 10.

Here is my R session info:

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
... 
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
other attached packages:
[1] scales_0.2.4  ggplot2_1.0.0

Upvotes: 2

Views: 1515

Answers (1)

Vlo
Vlo

Reputation: 3188

It doesn't much sense to apply a stat_cdf to geom_point(). If you want a CDF plot, you are better off with.

library(ggplot2)
df = data.frame(a=1:10)
ggplot(data = df, aes(x=a)) + stat_ecdf() + scale_x_discrete(breaks = 1:11)

enter image description here

stat_ecdf is supposed to be a step function.

If you insist on making your code work and would accept a hack-around, then you can do this.

ggplot(data = df, aes(x=a)) + geom_point(stat = "ecdf", colour = c(rep("red", 11), NA))

enter image description here

Upvotes: 4

Related Questions