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")
			
# 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!

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