Home » How to Plot a Binomial Distribution in R

How to Plot a Binomial Distribution in R

by Tutor Aspire

To plot the probability mass function for a binomial distribution in R, we can use the following functions:

  • dbinom(x, size, prob) to create the probability mass function
  • plot(x, y, type = ‘h’) to plot the probability mass function, specifying the plot to be a histogram (type=’h’)

To plot the probability mass function, we simply need to specify size (e.g. number of trials) and prob (e.g. probability of success on a given trial) in the dbinom() function.

For example, the following code illustrates how to plot a probability mass function for a binomial distribution with size = 20 and prob = 0.3:

success 

Plot of Binomial distribution probability mass function in R

The x-axis shows the number of successes and the y-axis shows the probability of obtaining that number of successes in 20 trials.

We can add a title, change the axes labels, and increase the line width to make the plot more aesthetically pleasing:

success 

Binomial distribution probably mass function plot in R

You can use the following code to obtain the actual probabilities for each number of successes shown in the plot:

#prevent R from displaying numbers in scientific notation 
options(scipen=999) 

#define range of successes
success #display probability of success for each number of trials
dbinom(success, size=20, prob=.3)

[1] 0.00079792266297612 0.00683933711122388 0.02784587252426865
[4] 0.07160367220526231 0.13042097437387065 0.17886305056987975
[7] 0.19163898275344257 0.16426198521723651 0.11439673970486122
[10] 0.06536956554563482 0.03081708090008504 0.01200665489613703
[13] 0.00385928193090119 0.00101783259716075 0.00021810698510587
[16] 0.00003738976887529 0.00000500755833151 0.00000050496386536
[19] 0.00000003606884753 0.00000000162716605 0.00000000003486784

Additional Resources

An Introduction to the Binomial Distribution
Understanding the Shape of a Binomial Distribution

You may also like