How can I use R to plot a binomial distribution?

R is a programming language and software environment commonly used for statistical analysis and data visualization. It offers a variety of functions and packages that allow users to easily create various types of graphs and plots, including binomial distributions.

To plot a binomial distribution using R, first, the user must define the parameters of the distribution, such as the number of trials and the probability of success. This can be done using the built-in function “dbinom()”. Next, the user can use the function “plot()” to create a visual representation of the distribution. This will generate a graph with the x-axis representing the number of successes and the y-axis representing the probability of each number of successes.

In addition to the basic plot, R also offers the option to add labels, change the color and style of the plot, and customize other features to enhance the visualization. With its user-friendly interface and robust capabilities, R provides a powerful tool for accurately and efficiently plotting binomial distributions for various statistical analysis purposes.

Plot a Binomial Distribution in R


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 <- 0:20

plot(success, dbinom(success, size=20, prob=.3),type='h')

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 <- 0:20

plot(success,dbinom(success,size=20,prob=.3),
     type='h',
     main='Binomial Distribution (n=20, p=0.3)',
     ylab='Probability',
     xlab ='# Successes',
     lwd=3)

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 <- 0:20

#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

x