ggplot practice

Lecture 5

Dr. Elijah Meyer

NC State University
ST 295 - Spring 2025

2025-01-21

Checklist

– Have you cloned the today’s AE repo?

– Have you cloned your homework repo?

– Are you keeping up with prepare material?

  > This includes making sure you can render a document to a PDF!
  

Announcements

Homework 1 is live!

– Summary stats & plotting

– Post questions on Moodle / Email

– You can work together, but submit individual work (don’t cheat and just copy answers)

– Turn in HW via PDF on Gradescope

Warm-up

Read this code as a sentence

library(tidyverse)

mtcars |>
  ggplot(
    aes(x = mpg)
  ) +
  geom_histogram()

Warm-up

Why write this code this way?

mtcars |>
  ggplot(
    aes(x = mpg)
  ) +
  geom_histogram()

Warm-up

Why not this way?

mtcars |>ggplot(aes(x = mpg)
  )+geom_histogram()

Warm-up

|> should always have a space before it and should typically be the last thing on a line.

– Same rule for the + sign

mtcars |>
  ggplot(
    aes(x = mpg)
  ) +
  geom_histogram()

Warm-up

– if you can’t fit all of the arguments to a function on to a single line, put each argument on its own line

flights |> 
  group_by(dest) |> 
  summarize(
    distance = mean(distance),
    speed = mean(distance / air_time, na.rm = TRUE)
  ) 

AE-2