3  Workflow: basics

Prerequisites

library(tidyverse)
#> ── Attaching core tidyverse packages ───────────────────── tidyverse 2.0.0 ──
#> ✔ dplyr     1.1.2     ✔ readr     2.1.4
#> ✔ forcats   1.0.0     ✔ stringr   1.5.0
#> ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
#> ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
#> ✔ purrr     1.0.1     
#> ── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

3.5 Exercises

  1. The variable is called my_variable while the next line calls it my_varıable, with an an ı (i without a dot) in the variable name. Therefore the error is due to the mismatch in variable names.

  2. The corrected code is provided below.

    library(tidyverse)
    
    ggplot(mpg, aes(x = displ, y = hwy)) + 
      geom_point() +
      geom_smooth(method = "lm")
    #> `geom_smooth()` using formula = 'y ~ x'

  3. Option + Shift + K / Alt + Shift + K pulls up keyboard shortcuts. Alternatively, you can get to the same place with Help > Keyboard Shortcuts Help.

  4. The my_bar_plot object is saved because that’s what was specified in the plot argument of the ggsave() call.

    my_bar_plot <- ggplot(mpg, aes(x = class)) +
      geom_bar()
    
    my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
      geom_point()
    
    ggsave(filename = "mpg-plot.png", plot = my_bar_plot)
    #> Saving 6 x 4 in image