Get access to all lessons in this course.
- Welcome to Fundamentals of R
- Update Everything
- Start a New Project
-
Data Wrangling and Analysis
- The Tidyverse
- Pipes
- select()
- mutate()
- filter()
- summarize()
- group_by() and summarize()
- arrange()
- Create a New Data Frame
- Bring it All Together (Data Wrangling)
-
Data Visualization
- The Grammar of Graphics
- Scatterplots
- Histograms
- Bar Charts
- Setting color and fill Aesthetic Properties
- Setting color and fill Scales
- Setting x and y Scales
- Adding Text to Plots
- Plot Labels
- Themes
- Facets
- Save Plots
- Bring it All Together (Data Visualization)
-
Quarto
- Quarto Overview
- YAML
- Text
- Code Chunks
- Tips for Working with Quarto
- Bring It All Together (Quarto)
-
Wrapping Up
- An Important Workflow Tip
Fundamentals of R
Histograms
This lesson is locked
This lesson is called Histograms, part of the Fundamentals of R course. This lesson is called Histograms, part of the Fundamentals of R 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.
You need to be signed-in to comment on this post. Login.