Skip to content
R for the Rest of Us Logo

This lesson is locked

Get access to all lessons in this course.

If the video is not playing correctly, you can watch it in a new window

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

View code shown in video
# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

penguins <- read_csv("penguins.csv")
			
# Bar Charts --------------------------------------------------------------

# There are two basic approaches to making bar charts, 
# both of which use geom_bar().

# Approach #1

# Use your full dataset.
# Only assign a variable to the x axis.
# Let ggplot use the default stat transformation (stat = "count") 
# to generate counts that it then plots on the y axis.

ggplot(data = penguins,
       mapping = aes(x = bill_length_mm)) +
  geom_bar()

# The default statistical transformation for geom_bar() is count. 
# This will give us the same result as our previous plot.

ggplot(data = penguins,
       mapping = aes(x = bill_length_mm)) +
  geom_bar(stat = "count")


# Approach #2

# Wrangle your data frame before plotting, creating a new data frame 
# in the process
# Assign variables to the x and y axes
# Use stat = "identity" to tell ggplot to use the data exactly as it is

# It's often easier to do our analysis work, save a data frame, 
# and then use this to plot.
# Let's recreate our penguin_bill_length_by_island data frame.

penguin_bill_length_by_island <- penguins |> 
  group_by(island) |> 
  summarize(mean_bill_length = mean(bill_length_mm, na.rm = TRUE)) |> 
  arrange(mean_bill_length)

# Then let's use this data frame to make a bar chart. 
# The stat = "identity" here tells ggplot to use the exact data points
# without any statistical transformations.

ggplot(data = penguin_bill_length_by_island,
       mapping = aes(x = island,
                     y = mean_bill_length)) +
  geom_bar(stat = "identity")

# We can also flip the x and y axes. 

ggplot(data = penguin_bill_length_by_island,
       mapping = aes(x = mean_bill_length,
                     y = island)) +
  geom_bar(stat = "identity") 

# The function coord_flip() will do the same thing.

ggplot(data = penguin_bill_length_by_island,
       mapping = aes(x = island,
                     y = mean_bill_length)) +
  geom_bar(stat = "identity") +
  coord_flip()

# We can also use geom_col(), which is the same as geom_bar(stat = "identity")

ggplot(data = penguin_bill_length_by_island,
       mapping = aes(x = island,
                     y = mean_bill_length)) +
  geom_col()

Your Turn

# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

penguins <- read_csv("penguins.csv")
			
# Bar Charts --------------------------------------------------------------

# Use the v1 approach to make a bar chart that shows a count of the number of penguins by species. 

# YOUR CODE HERE

# Use the v2 approach by doing the following:
# 1. Creating a new data frame called penguins_by_species that is a 
# count of the number of penguins by species
# 2. Plot your data frame using the v2 approach with geom_bar()

# YOUR CODE HERE

# Make the same graph as above, but use geom_col() instead of geom_bar()

# YOUR CODE HERE

Learn More

You can also find examples of code to make bar charts on the Data to Viz website , the R Graph Gallery website , and in Chapter 3 of the R Graphics Cookbook. Michael Toth also has a detailed blog post about making bar charts with ggplot.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.