I am not sure what you want to get out of such comparison. Before putting the two graphs together, I think your code may have some issues: 1) your cumsum(abs(x)/sum(abs(x)))
may not be correct, I replaced it with cumsum(abs(10-x)/sum(abs(10-x)))
.
Second, for binomial distribution, rbinom(1e5,1e5, 0.001)
will give you numbers not probabilities, I replaced it with rbinom(1e5,1e5, 0.001)/1e5
.
library(tidyverse)
df1<-tibble(x = sort(rnorm(1e5)),
cumulative = cumsum(abs(10-x)/sum(abs(10-x)))/2.5)
df2<-tibble(x1 = sort(rbinom(1e5,1e5, 0.001)/1e5))
ggplot(df1, aes(x=x)) +
geom_histogram(aes(y = ..density..), bins = 500)+
geom_density(color = "red")+
geom_line(aes(y = cumulative), color = "navy")+
scale_y_continuous(sec.axis = sec_axis(~.*2.5, name = "cumulative density")) +
geom_histogram(data = df2, aes(x = x1, y = ..density..), bins = 90)
This will produce:
You can change bins
to adjust height. However, we need to be careful with the interpretation of the difference between two distributions: one is the distribution of individuals with mean = 0 and SD =1 (normal distribution) while the other is the distribution of population estimates with a probability of 0.001 and sample size of 1e5.
ggplot(df1, aes(x=x)) +
geom_histogram(aes(y = ..density..), fill="red", bins = 15)+
geom_density(color = "red")+
geom_line(aes(y = cumulative), color = "navy")+
scale_y_continuous(sec.axis = sec_axis(~.*2.5, name = "cumulative density")) +
geom_histogram(data = df2, aes(x = x1, y = ..density..), color = "green", fill="green", bins = 15)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…