Skip to content
Coming soon: Ally. Your guide to the world of AI and R. Learn More →
R for the Rest of Us Logo

Fundamentals of R

Bar Charts

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 easily 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")

# 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.

Felipe Coelho

Felipe Coelho • March 22, 2026

Considering the code below:

penguins_by_species <- penguins |> 
  group_by(species) |>
  summarize(mean_penguins = n()) |>
  arrange(mean_penguins)
  

Would it be possible to include this line in the code: col_types = cols("mean_penguins" = col_integer())?

Gracielle Higino

Gracielle Higino Coach • March 24, 2026

Hi Felipe! the col_types is an argument of the read_csv() function, so it shouldn't work as a standalone thing, and wouldn't work either as a column transformation layer. If you want to change the type of the mean_penguins column, you should use mutate(), as in the example below:

penguins |>
  group_by(species) |>
  summarize(mean_penguins = n()) |>
  mutate(mean_penguins = as.character(mean_penguins))
Felipe Coelho

Felipe Coelho • March 26, 2026

Thank you, Gracielle!

J.R. Moller

J.R. Moller • March 23, 2026

Pending approval

The code provided under "View code in video" is the answers to the "Your turn" section instead of the code from the video.