Plot density and cumulative density function in on

2019-04-02 10:14发布

I would like to get a plot that combines the density of observations and the cdf.

The usual problem with that is that the scales of the two are way off. How can this be remedied, i.e., two scales be used or, alternatively, one of the data series be rescaled (preferably within ggplot, as I would like to separate computation and display of data).

Here's the code so far:

>dput(tmp) yields

structure(list(drivenkm = c(8, 11, 21, 4, 594, 179, 19, 7, 10, 36)), .Names = "drivenkm", class = c("data.table", "data.frame" ), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x223cb78>)

then I do

p = ggplot(data = tmp, aes(x = drivenkm)) + geom_histogram(aes(y = ..density..), alpha = 0.2, binwidth = 3) + stat_ecdf(aes(x = drivenkm)); print(p)

What I get is the following:

enter image description here

Obviously, the scales are way off. How can this be fixed, such that both the histogram and the cdf can be interpreted in a sensible way?

Thanks!

1条回答
迷人小祖宗
2楼-- · 2019-04-02 10:32

The density is scaled by the binwidth so the area sums to 1. So the y for your histogram should be multiplied by this too:

p = ggplot(data = tmp, aes(x = drivenkm)) +
   geom_histogram(aes(y = 3*..density..), alpha = 0.2, binwidth = 3) +
   stat_ecdf(aes(x = drivenkm))

enter image description here

查看更多
登录 后发表回答