How to Plot a Binomial Distribution in R

Spread the love

This article aims to guide you through plotting a binomial distribution in R and will cover an introduction to the binomial distribution, generating and plotting binomial data, and interpreting the resultant plots.

Introduction to Binomial Distributions

The binomial distribution is a probability distribution that describes the number of successes in a fixed number of independent Bernoulli trials, each with the same probability of success. The distribution is defined by two parameters: the number of trials (n) and the probability of success in a single trial (p).

The binomial distribution models the total number of successes in fixed-size samples drawn with replacement from a population of size N. If the sampling is carried out without replacement, the draws are not independent, and the resulting distribution is a hypergeometric distribution, not a binomial one.

Generating a Binomial Distribution in R

The first step in plotting a binomial distribution is to generate a binomially distributed dataset. You can use the rbinom() function in R to achieve this. rbinom() generates random deviates from a binomial distribution. Here’s an example of how to use it:

# Set seed for reproducibility

# Generate 1000 binomial random variables
binomial <- rbinom(1000, size = 10, prob = 0.5)

# Inspect the first 10 elements
head(binomial, 10)

In this example, we’re generating 1000 random variables from a binomial distribution with a size (number of trials) of 10 and a prob (probability of success on each trial) of 0.5. The set.seed() function is used to ensure the reproducibility of the random numbers.

Plotting a Binomial Distribution in R

After generating the binomial data, the next step is to create a plot to visualize it. Two common types of plots you might consider are histograms and bar plots.


A histogram is a graphical representation that organizes a group of data points into specified ranges. In R, you can use the hist() function to plot a histogram:

# Plot histogram
hist(binomial, main="Histogram of Binomial Distribution", xlab="Value", ylab="Frequency", col="lightblue", border="black")

This code creates a histogram where the x-axis represents the values of the random variable and the y-axis represents the frequency of these values.

Bar Plot

For discrete data like a binomial distribution, a bar plot might be more appropriate than a histogram. The table() function can be used to count the frequency of each outcome, and the barplot() function can then be used to display this:

# Create frequency table
binomial_freq <- table(binomial)

# Plot bar plot
barplot(binomial_freq, main="Bar Plot of Binomial Distribution", xlab="Value", ylab="Frequency", col="lightblue", border="black")

This code creates a bar plot with the same axes as the histogram.

Adding a Theoretical Curve

To confirm if our data follows a binomial distribution, we can overlay a theoretical binomial distribution on our plot. We use the dbinom() function, which gives the density (probabilities) of a binomial distribution for a sequence of values:

# Create sequence of values
x_values <- seq(min(binomial), max(binomial), by = 1)

# Calculate probabilities of theoretical distribution
y_values <- dbinom(x_values, size = 10, prob = 0.5)

# Plot histogram with theoretical curve
hist(binomial, freq=FALSE, main="Histogram with Theoretical Curve", xlab="Value", ylab="Probability", col="lightblue", border="black")
lines(x_values, y_values, col="darkred", lwd=2)

In this code, freq=FALSE is used to plot probabilities instead of frequencies in the histogram. The lines() function adds a theoretical binomial distribution curve to the histogram.

Interpreting the Plots

Once you’ve created your binomial distribution plot, the next step is understanding what the plot is telling you.

In both the histogram and bar plot, the x-axis shows the possible outcomes, and the y-axis shows the frequency or probability of each outcome.

The theoretical curve overlay provides a way to visually assess how well your data aligns with a binomial distribution. If your data closely follows this curve, it suggests that the binomial distribution is a good fit for your data.


In this article, we’ve gone through the steps of generating and plotting a binomial distribution in R. We started with an introduction to the binomial distribution, then covered how to generate a binomially distributed dataset. We then plotted this data as a histogram and bar plot, and added a theoretical binomial distribution curve. Finally, we discussed how to interpret these plots.

Posted in RTagged

Leave a Reply