Bar Charts
This lesson is called Bar Charts, part of the Fundamentals of R course. This lesson is called Bar Charts, part of the Fundamentals of R course.
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!
Course Content
34 Lessons
1
The Grammar of Graphics
04:39
2
Scatterplots
03:46
3
Histograms
05:47
4
Bar Charts
06:37
5
Setting color and fill Aesthetic Properties
02:39
6
Setting color and fill Scales
05:40
7
Setting x and y Scales
03:09
8
Adding Text to Plots
07:32
9
Plot Labels
03:57
10
Themes
02:19
11
Facets
03:12
12
Save Plots
02:57
13
Bring it All Together (Data Visualization)
06:42
You need to be signed-in to comment on this post. Login.
John LeMay • October 18, 2024
Why do the extra data wrangling for v2? Is it in case you want to do something besides a basic count for each bar? I am a bit confused on that.
David Keyes Founder • October 19, 2024
Good question! My experience is that you only rarely want to make a simple enough plot that the v1 approach works. Usually, I'm doing a bunch of filtering, summarizing, etc before I plot. In that case, it's easiest (at least for how my mind works) to do this, save it as an object, and then use this to plot. Hope that helps!
John LeMay • October 21, 2024
I get you! Real-life data is rarely that simplistic!