library(mxmaps)
library(viridis)
library(scales)
df_mxmunicipio$value <- df_mxmunicipio$indigenous / df_mxmunicipio$pop
gg = MXMunicipioChoropleth$new(df_mxmunicipio)
gg$title <- "Percentage of the population that self-identifies as indigenous"
gg$set_num_colors(1)
gg$ggplot_scale <- scale_fill_viridis("percent", labels = percent)
gg$render()
Another example with categorical data
library("ggplot2")
df_mxmunicipio$value <- as.factor(sample(c(NA, letters[1:6]),
nrow(df_mxmunicipio),
replace = TRUE) )
gg = MXMunicipioChoropleth$new(df_mxmunicipio)
gg$title <- "Municipios a-f"
gg$set_num_colors(6)
gg$set_zoom(subset(df_mxmunicipio, state_name %in% c("Ciudad de México", "México"))$region)
gg$ggplot_scale <- scale_fill_brewer("type", type = "qual", palette = 2,
na.value = "gray")
p <- gg$render()
p + theme_void()
You can also edit the ggplot object directly, for example, if you wanted to remove the municipio borders.
library("scales")
df_mxmunicipio$value <- df_mxmunicipio$pop
p <- mxmunicipio_choropleth(df_mxmunicipio,
title = "Total population, by municipio",
legend = "population",
num_colors = 9)
p[["layers"]][[1]][["aes_params"]][["colour"]] <- "transparent"
p
We can also add the names of important municipios to maps (in this case those municipios in Chihuahua with more than 100,000 persons)
library("ggrepel")
df_mxmunicipio$value <- df_mxmunicipio$indigenous / df_mxmunicipio$pop * 100
chih <- subset(df_mxmunicipio, state_name %in% c("Chihuahua"))
p <- mxmunicipio_choropleth(df_mxmunicipio, num_colors = 1,
zoom = chih$region,
title = "Percentage of the population that self-identifies\nas indigenous in Chihuahua",
show_states = FALSE,
legend = "%")
labels <- chih
labels$group <- NA
labels <- subset(labels, pop > 1e05)
p +
geom_text_repel(data = labels,
aes(long, lat, label = municipio_name),
nudge_x = .1,
nudge_y = .7) +
geom_point(data = labels,
aes(long, lat),
color = "#d6604d",
size = 1)