Creating a simple US map in R can be done in a number of ways. Two popular packages for this type of project are ggplot2 and plotly. In this case, I used plotly.
The data for my map is a list of US state codes (NE, IL, MA, CA, etc.). A second variable gives a count of how many players the Nebraska football team is targeting in each state. In order to follow my example with your own data, you will need to have the state code variable and some numeric variable to map it against.
Once you have your data in a table and are ready to use it, create the following styling options for the map, which we will apply later:
mapDetails <- list( scope = 'usa', projection = list(type = 'albers usa'), showlakes = TRUE, lakecolor = toRGB('white') )
As you may have guessed, “scope” determines the type of map, in this case a map of the USA. We will also determine here what to do with lakes and how to color them.
usaMap <- plot_geo(X2018targets, locationmode = 'USA-states') %>% add_trace( z = X2018targets$Targets, locations = X2018targets$`State Code`, color = X2018targets$Targets, colors = 'Blues' ) %>% colorbar(title = "Targets") %>% layout( title = '2018 Nebraska Football Targets by State (February 2018)', geo = g )
The code above connects my data to the map and allows me to modify text within the plot area. My data frame is called “X2018targets,” so you’ll need to replace this with your data frame name. You’ll also need to set “z” to your numeric data and “locations” to your state code variable.
When you’re finished, simply type “usaMap” and hit enter to see your plot appear (I use R Studio, by the way, assuming you likely do as well). If you have any trouble or questions, let me know in the comments.