Histograms
This lesson is called Histograms, part of the Fundamentals of R course. This lesson is called Histograms, 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.
Loading transcript...
View code shown in video
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# Histograms --------------------------------------------------------------
# We use geom_histogram() to make a histogram.
ggplot(
data = penguins,
mapping = aes(
x = bill_length_mm
)
) +
geom_histogram()
# How does ggplot know what to plot on the y axis?
# It's using the default statistical transformation for geom_histogram,
# which is stat = "bin".
# If we add stat = "bin" we get the same thing.
# Each geom has a default stat.
ggplot(
data = penguins,
mapping = aes(
x = bill_length_mm
)
) +
geom_histogram(stat = "bin")
# We can adjust the number of bins using the bins argument.
ggplot(
data = penguins,
mapping = aes(
x = bill_length_mm
)
) +
geom_histogram(bins = 100)
Your Turn
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# Histograms --------------------------------------------------------------
# Make a histogram that shows the distribution of the body_mass_g variable.
# YOUR CODE HERE
# Adjust your histogram so it has 50 bins.
# YOUR CODE HERE
Learn More
You can find examples of code to make histograms on the Data to Viz website , the R Graph Gallery website , and in Chapter 6 of the R Graphics Cookbook , and Chapter 7 of the Fundamentals of Data Visualization.
To learn about more statistical transformations, Chapter 9 of R for Data Science has a discussion of them.
Have any questions? Put them below and we will help you out!
Course Content
33 Lessons
1
The Grammar of Graphics
04:36
2
Scatterplots
03:40
3
Histograms
04:51
4
Bar Charts
04:53
5
Setting color and fill Aesthetic Properties
02:43
6
Setting color and fill Scales
05:12
7
Setting x and y Scales
02:58
8
Adding Text to Plots
05:50
9
Plot Labels
02:59
10
Themes
02:10
11
Facets
02:56
12
Save Plots
02:49
13
Bring it All Together (Data Visualization)
06:14
You need to be signed-in to comment on this post. Login.