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
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. State Code`, color = X2018targets$Targets, colors = ‘Blues’ ) %>% colorbar(title = “Targets”) %>% layout( title = ‘2018 Nebraska Football Targets by State (February 2018)’, geo = mapDetails )
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.
Hi!
Great article. I’m new to R. Could you please explain what — geo = g — is doing?
I tried to use your code, but R says “object ‘g’ not found”.
Best, Adri
Hi Adri — thanks for your comment and glad to hear you’re experimenting with R.
Looks like there may be an error in my example. The “geo = g” piece should determine the type and style of the map. I originally named this “mapDetails” but later refer to it as “g.” So, to correct this, you can probably just replace the “g” with “mapDetails” or rename “mapDetails” as “g.”
If that doesn’t work let me know and I’ll dabble with it.
Sorry for the confusion and nice catch. I’ll update the post soon as well.