Skip to content
What's New in R is a weekly email to help you up your R game. Sign up →
R for the Rest of Us Logo

Fundamentals of R

Scatterplots

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

# Scatterplots ------------------------------------------------------------

# We use geom_point() to make a scatterplot.

ggplot(data = penguins,
       mapping = aes(x = bill_length_mm,
                     y = bill_depth_mm)) +
  geom_point()

Your Turn

# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

penguins <- read_csv("penguins.csv")

# Scatterplots ------------------------------------------------------------

# Make a scatterplot that shows flipper length on the x axis and body mass on the y axis.

# YOUR CODE HERE

Learn More

Claus Wilke talks about scatterplots in Chapter 12 of his book Fundamentals of Data Visualization. Michael Toth also has a long blog post about all of the ins and outs of making scatterplots in ggplot.

You can also find examples of code to make scatterplots on the Data to Viz website , the R Graph Gallery website , and in Chapter 5 of the R Graphics Cookbook.

Have any questions? Put them below and we will help you out!

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

Leo Gutknecht-Gmeiner

Leo Gutknecht-Gmeiner • March 25, 2024

I would assume that all rows with NA in either of the variables will have to be dropped...

David Keyes

David Keyes Founder • March 25, 2024

Yup!

Patricia A Spencer

Patricia A Spencer • March 26, 2024

Is this how I would drop na? Thanks

penguins |> 
  drop_na(flipper_length_mm, body_mass_g) |> 
  ggplot(data = penguins,
         mapping = aes(x = flipper_length_mm,
         y = body_mass_g))+
    geom_point()
David Keyes

David Keyes Founder • March 26, 2024

Yes, that works! However, if you omit the drop_na() line, you'll get the same thing (try it out and see!) because ggplot will only add points for rows that have observations for both flipper_length_mm and body_mass_g.

Patricia A Spencer

Patricia A Spencer • March 26, 2024

Thanks, David!

Lilly Kennedy

Lilly Kennedy • April 23, 2024

My ggplot is not automatically configuring the right "zoom" of the scatterplot, it has values down to -1000. How do I fix that?

Libby Heeren

Libby Heeren Coach • April 23, 2024

Hi, Lilly! In this R project (fundamentals-v2), David is using a file called penguins.csv which already had the -999 values cleaned so that they're NA values. If you're using the penguins_data.csv file from previous lessons, you'd need to clean those -999 values before plotting. The video where he shows how he cleaned those values is here in the week 1 video called Import Our Data Again.

Derrick Watsala

Derrick Watsala • June 13, 2024

In other words, the scatter plot will still be generated even if there NAs in the variables to be plotted?

David Keyes

David Keyes Founder • June 13, 2024

Yes, exactly!