Activity: Creating a Variable-Encoded Regional Map

Steps for Completion:

  1. Merge the USStates data with states_map.
  2. Before merging, change the states variable in USStates to the same format used in states_map.
  1. Use the ggplot options geom_polygon and coord_map to create the map.
  2. For aesthetics, run the following code and specify x=long, y=latgroup=group, and fill=ObamaVote.

Outcome:

The complete code is shown as follows:

USStates$Statelower <- as.character(tolower(USStates$State))glimpse(USStates)us_data <- merge(USStates,states_map,by.x="Statelower",by.y="region")head(us_data)

Get Applied Data Visualization with R and ggplot2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.