Creating Bar Plots and Histograms
Creating Bar Plots and Histograms
Welcome back! In the last lesson, you learned how to make your plots more informative by adding labels and titles. Now, it's time to expand your skills by creating different types of visualizations — specifically, bar plots and histograms. These types of plots are incredibly useful for summarizing categorical and continuous data.
What You'll Learn
In this unit, you’ll learn how to create bar plots and histograms using ggplot2. We'll start with a simple bar plot to show the count of different species in the iris dataset. Then, we'll move on to creating a histogram to represent the distribution of Sepal Length.
Bar Plots
Bar plots are a straightforward way to visualize categorical data. They display rectangular bars with lengths proportional to the values they represent. Here, we'll use the geom_bar function to create a bar plot of species counts in the iris dataset.
By the end of this lesson, you'll be able to generate a basic bar plot like the one shown below:

Here's the code to create a bar plot:
Let's break down the code:
data(iris)loads iris datasetggplot(iris, aes(x = Species))initializes the plot with theirisdataset and sets the x-aesthetic to theSpeciesvariable.geom_bar()tells ggplot2 to create a bar plot.theme_light()applies a lighter theme to the plot.labs(title = "Count of Species")adds a title to the plot.
Histograms
Histograms are used to visualize the distribution of a continuous variable. They divide the data into bins and display the count of data points in each bin. We'll use the geom_histogram function to create a histogram of Sepal Length, filled by species and adjusted with specific bin widths.
By the end of this lesson, you'll be able to generate a basic histogram like the one shown below:

Here's the code to create a histogram:
Let's break down this code:
data(iris)loads iris datasetggplot(iris, aes(x = Sepal.Length, fill = Species))initializes the plot with theirisdataset and sets the x-aesthetic toSepal.Lengthand the fill toSpecies. Thefillaesthetic is used to color the bars based on theSpeciesvariable, making it easier to distinguish between different species within the distribution.geom_histogram(binwidth = 0.5, position = "dodge")creates a histogram with bins of width 0.5 and dodges the bars by species so they don’t overlap.theme_light()applies a lighter theme to the plot.labs(title = "Distribution of Sepal Length")adds a title to the plot.
