Note: There are often multiple ways to answer each question.

  1. Load the ggplot2, dplyr and maps packages and save the world map data frame into the variable map_world. (Hint: Look at this PDF for hints on where the world map data frame is saved.)
library(ggplot2)
library(dplyr)
library(maps)
map_world <- map_data("world")
  1. Plot the world map, ensuring that the aspect ratio of the map is correct and that the countries have white fill and black outline.
ggplot(map_world) + 
    geom_polygon(aes(x = long, y = lat, group = group), 
                 col = "black", fill = "white") +
    coord_quickmap()

  1. Save the coordinates for just the region “China” in the variable map_china. Plot with the same settings as above, except that in addition, we don’t see the latitude and longitude axes.
map_china <- map_world %>% filter(region == "China")
map_theme <- theme(
    axis.title.x = element_blank(),
    axis.text.x  = element_blank(),
    axis.ticks.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y  = element_blank(),
    axis.ticks.y = element_blank()
)
ggplot(map_china) + 
    geom_polygon(aes(x = long, y = lat, group = group), 
                 col = "black", fill = "white") +
    coord_quickmap() +
    map_theme

  1. Load US state-level data into the variable map_state. Plot just the states of California, Oregon and Washington.
map_state <- map_data("state")
map_state %>% 
    filter(region %in% c("california", "oregon", "washington")) %>%
    ggplot() +
        geom_polygon(aes(x = long, y = lat, group = group)) +
        coord_quickmap()

  1. Load the datasets package. (The dataset package contains variables state.name and state.division which we will use.) Create a data frame named division such that the first column region contains state.name in lower case, and the second column division contains state.division.
library(datasets)
division <- data.frame(region = tolower(state.name), division = state.division)
head(division)
##       region           division
## 1    alabama East South Central
## 2     alaska            Pacific
## 3    arizona           Mountain
## 4   arkansas West South Central
## 5 california            Pacific
## 6   colorado           Mountain
  1. Join division to map_state (with map_state on the left) on the key region.
map_state2 <- map_state %>% left_join(division, by = "region")
## Warning: Column `region` joining character vector and factor, coercing into
## character vector
head(map_state2)
##        long      lat group order  region subregion           division
## 1 -87.46201 30.38968     1     1 alabama      <NA> East South Central
## 2 -87.48493 30.37249     1     2 alabama      <NA> East South Central
## 3 -87.52503 30.37249     1     3 alabama      <NA> East South Central
## 4 -87.53076 30.33239     1     4 alabama      <NA> East South Central
## 5 -87.57087 30.32665     1     5 alabama      <NA> East South Central
## 6 -87.58806 30.32665     1     6 alabama      <NA> East South Central
  1. Plot a map of the US at state-level, with the fill of the states indicating the division it belongs to.
ggplot(map_state2) +
    geom_polygon(aes(x = long, y = lat, group = region, fill = division)) +
    coord_quickmap()

  1. Figure out why there is an NA in the legend in the map resulting from Question 7. Modify the data frame so that the NA is not included.

The NAs are due to the fact that the District of Columbia does not appear in the division dataset. We can remove all rows related to the District of Columbia to get rid of the NA:

map_state2 %>% filter(!is.na(division)) %>%
    ggplot() +
        geom_polygon(aes(x = long, y = lat, group = region, fill = division)) +
        coord_quickmap()

  1. Augment the map from Question 7 with state abbreviations in the center of the map, using state.abb and state.center to help you. (Hint: Combine state.abb and state.center into an auxiliary data frame first.)
state_name <- data.frame(abb = state.abb, x = state.center$x, y = state.center$y)
ggplot() +
    geom_polygon(data = map_state2, 
                 aes(x = long, y = lat, group = region, fill = division)) +
    geom_text(data = state_name, aes(x = x, y = y, label = abb)) +
    coord_quickmap()

  1. You may have noticed that the state abbreviations for Hawaii and Alaska were included, even though they were not drawn. (This of course depends on how you drew the map in Question 9.) Redo the map above, but without plotting the abbreviations for these two states.
state_name2 <- state_name %>% filter(abb != "HI" & abb != "AK")
ggplot() +
    geom_polygon(data = map_state2, 
                 aes(x = long, y = lat, group = region, fill = division)) +
    geom_text(data = state_name2, aes(x = x, y = y, label = abb)) +
    coord_quickmap()