GGPLOT CHALLENGE
We’ll use the penguins dataset to put your new R and ggplot skills to the test!
Load necessary libraries
Remember, if it’s the first time using any package, you’ll need to install the package first install.packages(“name_of_package”) before calling the library function
library(tidyverse)
library(palmerpenguins)
library(ggeasy) # this may be new to you, try googling it if you are unfamiliar with this package! First we’ll take a look at the penguins dataset
Let’s create a new variable called my_penguins to save this dataset to
head(penguins)## # A tibble: 6 x 8
## species island bill_length_mm bill_depth_mm flipper_length_~ body_mass_g sex
## <fct> <fct> <dbl> <dbl> <int> <int> <fct>
## 1 Adelie Torge~ 39.1 18.7 181 3750 male
## 2 Adelie Torge~ 39.5 17.4 186 3800 fema~
## 3 Adelie Torge~ 40.3 18 195 3250 fema~
## 4 Adelie Torge~ NA NA NA NA <NA>
## 5 Adelie Torge~ 36.7 19.3 193 3450 fema~
## 6 Adelie Torge~ 39.3 20.6 190 3650 male
## # ... with 1 more variable: year <int>
my_penguins <- penguins # once you run this line of code, you will see my_penguins appear in your global environmentGetting to know the data
We went through this in Tuesday’s QnA session, so I’m including the code below, but see if you can answer the questions on your own!
- How many species of penguins are included in this dataset? What are their names?
- What is the range of the years?
- How many islands are there? What are they?
levels(my_penguins$species) # levels() only works for factor variables ## [1] "Adelie" "Chinstrap" "Gentoo"
unique(my_penguins$species)## [1] Adelie Gentoo Chinstrap
## Levels: Adelie Chinstrap Gentoo
my_penguins$year <- as.numeric(as.character(my_penguins$year))
range(my_penguins$year)## [1] 2007 2009
levels(my_penguins$island)## [1] "Biscoe" "Dream" "Torgersen"
Challenge Time!
Below you’ll see a series of plots generated from this dataset. Your task is simple…reproduce the plots! Okay, maybe it’s not such a simple task, but give it a go.