Introduction and Overview

Welcome to this interactive lesson on bar plots and histograms in R! In this lesson, we will embark on a beautiful journey through data visualization. We will focus on constructing bar plots and histograms using ggplot2. Are you ready? Let's begin!

Building Bar Plots with `ggplot2`

A bar plot visually represents categorical data as rectangular bars, the lengths of which are proportional to their respective values. For instance, a bar plot is an ideal choice if we want to visualize a bookstore's sales data, where the categories are book names and the values are the sales numbers.

We can build a bar plot using the geom_bar() function from ggplot2. Observe the following example:

library(ggplot2)

books <- c('Book1', 'Book2', 'Book3', 'Book4', 'Book5')   # Book names
sales <- c(123, 432, 567, 245, 312)            # Corresponding number of copies sold
data <- data.frame(books, sales)

plot <- ggplot(data, aes(x=books, y=sales)) + geom_bar(stat="identity", color="black", fill="lightblue") +
  labs(title="Book Sales", x="Books", y="Number of Sold Copies")

Let's break down the arguments for the geom_bar function:

  • The stat argument in the geom_bar function specifies the statistical transformation for this layer to use on the data. In the provided example, we use stat="identity", which means that the heights of the bars are set to the values in the data. By default, geom_bar() uses stat="count", which counts the number of cases at each x position and plots a bar with the corresponding height.
  • color: Defines the color of the bar's edges. Here, the edges are colored black.
  • fill: Sets the fill color of the bars. In this case, the bars are filled with lightblue.

Building Histograms with `ggplot2`: Dataset

Now, let's move on to histograms! Unlike bar plots, histograms are designed for visualizing the distributions of continuous, numeric data. In a histogram, bars represent the frequency of data points that fall under specific ranges or bins. Let's generate some normal distributions using the rnorm function in R.

R
set.seed(123)  # for reproducible results
ages <- rnorm(n=150, mean=27, sd=12)
Building Histograms with `ggplot2`
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