2016-10-29 85 views
1

我該如何去製作一個帶有不同方式的兩個二項式pdf的圖形,並用陰影或任何其他方式填充重疊?2 R中的二項分佈重疊

+0

見http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/#histogram-and -density-plots-with-multiple-group – Bernhard

+0

我不知道你的意思是「二項分佈圖」,但也許這是你想要的? http://stackoverflow.com/questions/6957549/overlaying-histograms-with-ggplot2-in-r –

回答

1

試試這個:

df <- rbind(data.frame(binom=rbinom(1000, 20, 0.2), binomial.p='0.2') , data.frame(binom=rbinom(1000, 20, 0.5),binomial.p='0.5')) 
library(ggplot2) 
ggplot(df, aes(binom, fill=binomial.p)) + 
geom_histogram(position = 'dodge', binwidth = 1) + 
xlab('Number of success (n=20)') 

enter image description here

ggplot(df, aes(binom, fill=binomial.p)) + 
geom_density(alpha=0.2) + 
xlab('Number of success (n=20)') 

enter image description here