Search code examples
rvisualizationbinomial-theorem

2 Binomial Distributions Overlap in R


How would I go about making a graph of two binomial pdfs with different means and have the overlap be filled in with shading or anything for that matter?


Solution

  • Try this:

    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