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