Interactive maps with R

Part of MS Víctor Gauto seminar Better maps with coding for Gulich Institute

Show code
library(ggiraph)
library(ggplot2)
library(showtext)
library(ggtext)
library(glue)

font_add_google(name = "Tinos", family = "tinos")
showtext_auto()
showtext_opts(dpi = 300)

arg <- sf::st_read("vector/arg_simp.gpkg", quiet = TRUE)

p <- ggplot() +
  geom_sf_interactive(
    data = arg,
    aes(
      tooltip = Provincia,
      data_id = Provincia,
      onclick = sprintf(
        "window.open(\"http://es.wikipedia.org/wiki/%s\")",
        paste0("Provincia_de_", gsub(" ", "_", Provincia))
      ),
    ),
    fill = "#75aee0",
    color = "#e5e5e5",
    linewidth = 15
  ) +
  annotate(
    geom = "richtext",
    x = I(c(-1.7, -1.7, -1.7)),
    y = I(c(.9, .8, .1)),
    hjust = 0,
    label = c(
      "República <b style='color: #75aee0;'>Argentina</b>",
      "Click en cualquier provincia para<br>visitar su página en <b style='color: #f6b40e'>Wikipedia</b>",
      "**Víctor Gauto | @vhgauto**"
    ),
    fill = c(NA, NA, "#75aee0"),
    label.color = NA,
    label.r = unit(0, "line"),
    label.padding = unit(20, "lines"),
    size = c(800, 500, 300),
    color = c("black", "black", "black"),
    family = "tinos"
  ) +
  coord_sf(clip = "off") +
  theme_void(base_family = "tinos") +
  theme_sub_plot(
    margin = margin(l = 25000),
    title = element_text(hjust = 0),
    subtitle = ggtext::element_markdown()
  )

girafe(
  ggobj = p,
  width_svg = 1500,
  height_svg = 600,
  bg = "#e5e5e5",
  options = list(
    opts_tooltip(
      css = glue(
        "background-color:white;color:black;padding:5px;",
        "font-weight:bold;font-size:15pt;border-radius:0px;",
        "border-style:solid;border-color:black;border-width:1px;",
        "font-family:Lato;"
      ),
      opacity = 1
    ),
    opts_hover(
      css = girafe_css(
        css = "",
        area = glue("fill:#f6b40e;")
      )
    ),
    opts_hover_inv(css = "opacity:1"),
    opts_toolbar(saveaspng = FALSE)
  )
)
Show code
library(terra)

plet(
  vect(arg),
  col = "#74acdf",
  border = "white",
  lwd = 2,
  alpha = 1
)
Show code
library(leaflet)
library(leafem)
library(leaflet.extras)

leaflet(sf::st_transform(arg, 4326)) |>
  setView(
    lng = -67.21083,
    lat = -40.41021,
    zoom = 4
  ) |>
  addTiles(group = "OSM (default)") |>
  addProviderTiles(
    providers$CartoDB.Positron,
    group = "Positron (minimal)"
  ) |>
  addProviderTiles(
    providers$Esri.WorldImagery,
    group = "World Imagery (satellite)"
  ) |>
  addPolygons(
    fillColor = "#75aee0",
    fillOpacity = 1,
    stroke = TRUE,
    weight = .6,
    color = "white",
    opacity = 1,
    label = ~ paste0(
      "Provincia: ",
      Provincia
    ),
    group = "Provincias de Argentina",
    highlightOptions = highlightOptions(
      fillColor = "#f6b40e"
    )
  ) |>
  addFullscreenControl(
    position = "bottomright"
  ) |>
  addLogo(
    img = "img/ig.png",
    url = "https://ig.conae.unc.edu.ar/",
    width = round(330 / 3),
    height = round(90 / 3),
    position = "bottomleft"
  ) |>
  addResetMapButton() |>
  addLayersControl(
    baseGroups = c(
      "OSM (default)",
      "Positron (minimal)",
      "World Imagery (satellite)"
    ),
    overlayGroups = "Provincias de Argentina",
    options = layersControlOptions(
      collapsed = FALSE,
      position = "topright"
    )
  )
Show code
library(tmap)
library(tmap.mapgl)

tmap_mode("maplibre")

tm_shape(arg) +
  tm_polygons(
    lwd = 2,
    fill = "#75aee0",
    hover = "Provincia",
    popup.vars = FALSE,
    col = "white"
  ) +
  tm_fullscreen()
Show code
library(mapview)
pop <- arg$Provincia |>
  tolower() |>
  gsub(" ", "", x = _)

pop <- dplyr::if_else(stringr::str_detect(pop, "fuego"), "tierradelfuego", pop)
pop <- dplyr::if_else(stringr::str_detect(pop, "ciudad"), "caba", pop)
pop <- stringr::str_replace_all(
  pop,
  c("í" = "i", "ó" = "o", "é" = "e", "á" = "a")
)
pop <- paste0(
  "<i class='icono-arg-provincia-",
  pop,
  "' style='font-size: 3em;'></i>"
)
arg$Forma <- pop

mapview(
  arg,
  legend = FALSE,
  col.regions = "#75aee0",
  color = "white",
  lwd = 1,
  alpha.regions = 1,
  label = "Provincia",
  highlight = list(
    stroke = TRUE,
    color = "white",
    weight = 1,
    opacity = 1,
    fill = TRUE,
    fillColor = "#f6b40e",
    fillOpacity = 1
  ),
  popup = leafpop::popupTable(
    arg,
    zcol = "Forma",
    row.numbers = FALSE,
    feature.id = FALSE
  )
)



Ver en .