This tutorial aims to demonstrate step by step tutorial to successfully build a choropleth map.
In this example, we have chosen to plot the population of the United States in 2014 by state.
Firstly, using map library we can get the us_map data that will be later used to plot the US map.
Secondly, we can read the csv files of the us-state-populations data from 2014
us_map <- map_data("state")
us_state_population <- read.csv("~/COMP_STAT_112 - Tenzin/MidTerm/us-state-populations.csv")
First we need to ensure that we have calculated or manipulated the data we want to plot on the map correctly before plotting it on a map. In this case, we are plotting a choropleth map of the US’s population by states.
# to ensure same state name, we will make the name of all states in lower case.
us_state_population$state <- tolower(us_state_population$state)
# the final table is a left join table of US map data that contains latitude, longitude of states, and the US population by state
final_population_us_map <- left_join(us_map, us_state_population, by = c("region" = "state"))
The goal of this choropleth includes the map have interaction with the user. state_population_info is a data where we can define the information that we want to show the user when it hovers over each state.
# To ensure the interactivity of the choropleth map, we will create a state_population_info that will describe each state and its population information.
state_populatin_info <- paste0(
"\nregion: ", final_population_us_map$region,
"\nPopulation in millions: ", final_population_us_map$pop_2014
)
For choropleth purpose, we want to define population size by colors since the US population by state ranges from around 500K to over 38 million in a state. This breaks can be redefined and adjusted. The breaks and the labels has to be correponding to each other.
# Define breaks and labels for population categories
breaks <- c(0, 1e6, 10e6, 20e6, 30e6, 100e6)
labels <- c("<= 1 M", "<= 10 M", "<= 20 M", "<= 30 M", "<= 100 M")
After defining the breaks, and the labels. We will introduce these categories in the final_population_us_map table so that we can plot it. cut is a function that allows us to enter the row value by evaluating the pop_2014 values by breaks and assign it with the label.
# introduce population size category by using cut function by definning breaks and its corresponding labels
final_population_us_map$PopulationCategory <- cut(final_population_us_map$pop_2014, breaks = breaks, labels = labels)
To construct the map we rely plotting the map data using geom_polyon on a ggplot. In geom_polygon, we can define the map (the US map) data, and fill the map by value of Population Category by each state and create a clear demarcation black color between the border of the states.
grayscale_values <- c("#F0F0F0", "#CCCCCC", "#A0A0A0", "#707070", "#404040")
us_population_map <- ggplot() +
geom_polygon(data = us_map,
aes(x = long, y = lat, group = group, fill = final_population_us_map$PopulationCategory, label = state_populatin_info),
color = "black") +
scale_fill_manual(values = grayscale_values, name = "") +
labs(title = "US Population by State in 2014",
subtitle = "This map is a visual representation of population distribution in the US",
summary = "Califonia is the most populated States in the US",
caption = "Tenzin Gyaltsen (12/5/2023), Data source: [https://gist.github.com/bradoyler]") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.caption = element_text(hjust = 0.7)) +
coord_cartesian(xlim = c(-123.000000, -65.934570), ylim = c(24.396308, 49.384358))
## Warning in geom_polygon(data = us_map, aes(x = long, y = lat, group = group, :
## Ignoring unknown aesthetics: label
us_population_map
Using plotly, we can plot an interactive map with allows us to hover on the map to each states and show us relevant information by the label we defined in the us_population_map
# plotly library allows us to plot the map (us_population_map) that allows interaction and allow us to interact by hovering over the map
ggplotly(us_population_map, tooltip = "label") %>% style(hoveron = "point")
[ us-population-data ] (https://gist.github.com/bradoyler/0fd473541083cfa9ea6b5da57b08461c#file-us-state-populations-csv)
[ plotly library ] (https://plotly.com/r/choropleth-maps/)