knitr::opts_chunk$set(echo = TRUE, 
                      fig.dim = c(6, 6), 
                      dpi = 100, 
                      out.width = '100%')
library(mxmaps)
library(leaflet) # for colorNumeric
library(scales) # needed for comma

df_mxstate_2020$value <- df_mxstate_2020$afromexican / df_mxstate_2020$pop
pal <- colorNumeric("Blues", domain = df_mxstate_2020$value)
mxstate_leaflet(df_mxstate_2020,
                pal,
                ~ pal(value),
                ~ sprintf("State: %s<br/>Percent Afro-Mexican: %s",
                          state_name, comma(value))) %>%
  addLegend(position = "bottomright", 
            pal = pal, 
            values = df_mxstate_2020$value,
            title = "Percent<br>Afro-Mexican",
            labFormat = labelFormat(suffix = "%",
                                    transform = function(x) {100 * x})) %>%
  addProviderTiles("CartoDB.Positron")

You can also create maps with categorical variables by first converting the variables to integers and using the colors and labels parameters to addLegend to assign the colors to each level in the variable.

library(mxmaps)
library(leaflet) # for colorBin
library(scales) # needed for comma

df_mxstate_2020$value2 <- factor(sample(c("a", "b", "c", "d", "e"), 32, replace = TRUE))
df_mxstate_2020$value <- as.integer(df_mxstate_2020$value2)

pal <- colorBin(rainbow(5), domain = as.integer(factor(df_mxstate_2020$value2)))
mxstate_leaflet(df_mxstate_2020,
                pal,
                ~ pal(value),
                ~ sprintf("State: %s<br/>Percent Afro-Mexican: %s",
                          state_name, comma(value))) %>%
  addLegend(position = "bottomright", 
            colors =c(rainbow(5)),
            labels= c("a", "b","c","d", "e"),
            title = "categoría"
  ) %>%
  addProviderTiles("CartoDB.Positron")


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