knitr::opts_chunk$set(echo = TRUE, 
                      fig.dim = c(6,4), 
                      dpi = 200, 
                      out.width = '100%')
library(mxmaps)

data("df_mxstate_2020")
df_mxstate_2020$value <- df_mxstate_2020$pop
mxstate_choropleth(df_mxstate_2020,
                    title = "Total population, by state") 

Different color scale

library(mxmaps)
library(viridis)
library(scales)

df_mxstate_2020$value <-  df_mxstate_2020$indigenous_language / 
  df_mxstate_2020$pop 
gg = MXStateChoropleth$new(df_mxstate_2020)
gg$title <- "Percentage of the population that speaks\nan indigenous language"
gg$set_num_colors(1)
gg$ggplot_scale <- scale_fill_viridis("percent", labels = percent)
gg$render()

Map with labels for each state

library("geojsonio")
library("ggplot2")
library("rgdal")
library("rgeos")
library("maptools")
library("ggrepel")
library("sf")

df_mxstate_2020$value <-  df_mxstate_2020$indigenous_language / 
  df_mxstate_2020$pop * 100
p <- mxstate_choropleth(df_mxstate_2020, 
                        num_colors = 1,
                        title = "Percentage of the population that speaks\nan indigenous language",
                        legend = "%")


data("mxstate.topoJSON")
data("df_mxstate_2020")

tmpdir <- tempdir()
# have to use RJSONIO or else the topojson isn't valid
write(RJSONIO::toJSON(mxstate.topoJSON), file.path(tmpdir, "state.topojson"))
states <- topojson_read(file.path(tmpdir, "state.topojson"))
# convert to SpatialPolygonsDataFrame
states <- as_Spatial(states)

# make sure the coordinates of the labels are in the correct order
df_mxstate_2020 <- cbind(df_mxstate_2020, 
                    data.frame(lon = coordinates(states[match(df_mxstate_2020$region, states$id),])[,1],
                               lat = coordinates(states[match(df_mxstate_2020$region, states$id),])[,2]))
df_mxstate_2020$group <- df_mxstate_2020$state_abbr

p +
  geom_text_repel(data = df_mxstate_2020, 
                  aes(lon, lat, label = state_abbr,), 
                  size = 3,
                  box.padding = unit(0.1, 'lines'), 
                  force = 0.1)

Legends

Add a legend for states with NA values

df_mxstate_2020$value <-  df_mxstate_2020$indigenous_language / 
  df_mxstate_2020$pop * 100
df_mxstate_2020$value[1:5] <- NA
mxstate_choropleth(df_mxstate_2020, 
                   num_colors = 1,
                   title = "Percentage of the population that speaks\nan indigenous language",
                   legend = "%") +
  # Add a fake color scale which we'll change to 'no data'
  geom_point(data = df_mxmunicipio_2020[1,], 
             size = -1,
             aes(color = "",
                 group = NA, lat = lat, long = long)) +
  scale_color_manual(values = NA) +
  guides(color=guide_legend("no data", 
                             override.aes=list(color = "black",
                                               shape = 15, 
                                               size = 7)))


diegovalle/mxmaps documentation built on Sept. 22, 2023, 9:57 p.m.