Lab 3: Basic Data Visualizations

Question

Use the library() function to load the tidyverse package. Then run search(), and confirm that you see package:tidyverse in the output.

 [1] ".GlobalEnv"        "package:lubridate" "package:forcats"  
 [4] "package:stringr"   "package:dplyr"     "package:purrr"    
 [7] "package:readr"     "package:tidyr"     "package:tibble"   
[10] "package:ggplot2"   "package:tidyverse" "package:stats"    
[13] "package:graphics"  "package:grDevices" "package:utils"    
[16] "package:datasets"  "package:methods"   "Autoloads"        
[19] "package:base"     

It’s worth reflecting for a moment on the distinction between installing and loading a package. By way of analogy, consider the difference between buying a book and reading it.

  • Installing a package is like buying a book and putting it on your bookshelf. Once you have it, you don’t need to buy another copy. So you only have to install a package once. You may eventually need to upgrade the book if a 2nd edition comes out, but once it’s on your bookshelf, it’s always available to you if you want it. However, the book is still on your bookshelf and not in your brain. Therefore, you can’t actually use it.
  • Loading a package (via the library() function) is like taking the book off the shelf, placing it on your desk and opening it up. Now all of the information in that book is available to you. Once it’s on your desk, it’s available to you for as long as the book is on your desk. However, when you close your R session, all the books go back on the shelf! So you have to library() a package each time you open a new R session.

At the same time, you don’t want to library() more packages than you need, since then your desk will become cluttered.

Question

Use the library() function to load the palmerpenguins package. Then run search(), and confirm that you see package:palmerpenguins in the output.

 [1] ".GlobalEnv"             "package:palmerpenguins" "package:lubridate"     
 [4] "package:forcats"        "package:stringr"        "package:dplyr"         
 [7] "package:purrr"          "package:readr"          "package:tidyr"         
[10] "package:tibble"         "package:ggplot2"        "package:tidyverse"     
[13] "package:stats"          "package:graphics"       "package:grDevices"     
[16] "package:utils"          "package:datasets"       "package:methods"       
[19] "Autoloads"              "package:base"          
Question

Modify the code snippet above to create a histogram for the bill_length_mm variable.

Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).

Question

Use the geom_density() function to modify the code snippet above to create a density plot for the bill_length_mm variable.

Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_density()`).

Question

Use the geom_density() function to create a density plot for the bill_length_mm variable, and use the fill aesthetic to separate the distributions by island.

Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_density()`).

Question

Use the facet_wrap() function to create a density plot for the bill_length_mm variable, separating the facets by island.

Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_density()`).

Question

You can add more than one geom_*() to a plot. There are lots of different geom_*() functions. Experiment with a couple of other options.

Question

You can map additional aesthetics. geom_point() is perhaps the most versatile mark. In addition to x, y, color, and fill, it also understands alpha, shape, size, stroke, and group. Experiment by mapping a couple of these aesthetics to variables.

Question

You can map the same variable to multiple aesthetics. For example, you can map species to both the color and fill aesthetic. Try this or another double mapping.

Question

Use ggplot2 to create a plot that looks like ?@fig-example.

Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).