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:

# Load built-in dataset
data(iris)

# Bar plot of Species count
bar_plot <- ggplot(iris, aes(x = Species)) +
  geom_bar() +
  theme_light() +
  labs(title = "Count of Species")

Let's break down the code:

  • data(iris) loads iris dataset
  • ggplot(iris, aes(x = Species)) initializes the plot with the iris dataset and sets the x-aesthetic to the Species variable.
  • 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:

# Load built-in dataset
data(iris)

# Histogram of Sepal Length
hist_plot <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_histogram(binwidth = 0.5, position = "dodge") +
  theme_light() +
  labs(title = "Distribution of Sepal Length")

Let's break down this code:

  • data(iris) loads iris dataset
  • ggplot(iris, aes(x = Sepal.Length, fill = Species)) initializes the plot with the iris dataset and sets the x-aesthetic to Sepal.Length and the fill to Species. The fill aesthetic is used to color the bars based on the Species variable, 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.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal