The packages you will need for this AppEx are listed below. You may need to install some of them. If so, install the appropriate packages in the Console!
library(tidyverse)
library(sf)
library(spData)
library(RColorBrewer)
Today’s data set comes from the Kaggle, and contains the locations of each Chipotle location in the US as of 07/27/2020. Someone scraped this data from Chipotle.
The variables are
state: county namelocation: number of registered votersaddress: number of individuals who have votedlatitude: number of mail ballots returned and
acceptedlongitude: number of mail ballots rejectedWe will also use the us_states sf object
from the spData library to map the contiguous US.
chipotle <- read.csv("data/chipotle_stores.csv")
data("us_states")
Let’s map the US! Replace the underscores with the sf
object stored as us_states. Then add the appropriate
geom_xx() layer. Make it such that each state is outlined
in a color of your choice!
ggplot(_____) +
## add geom layer here +
labs(x = "Longitude", y = "Latitude")
Now let’s use the chipotle data to create a
visualization the locations of each store. Copy and paste your code from
Exercise 1 and then replace the underscores appropriately.
# code from Exercise 1 +
geom_point(data = ____, aes(x = ___, y = ____), size = 0.1)
Create a new data frame chipotle_counts that contains
the count of the number of Chipotle locations for each
state.
Then, create a new data frame called chipotle_state by
using an appropriate join between us_states and
chipotle_counts.
# wrangle!
Time to visualize! Using chipotle state, visualize the
map of the US where each state is filled in by its number of Chipotle
locations. Here, you will see if you chose the appropriate join
function. If something doesn’t look right, go back to Exercise 3 and
choose a different join!
# map!
Copy and paste the plotting code in Exercise 4 below. Using the
brewer.pal() function from the library
RColorBrewer, choose a different color palette. You must
pass in two arguments:
n: the number of different colors in the palette
(3-maximum).
name: the name of the palette. Select one of (in
quotes): BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn,
Spectral.
n is 11.# paste your code from Ex. 4 here +
scale_fill_gradientn(colors = brewer.pal(n = __, name = "___"))