7 min read

Map of Central America


Map of Central America

View raw source for this post

Summary

This blog post shows how to create a map of Central America using ggplot2 and sf.

Table of Contents

Overview

This blog post shows how to create a map of Central America using ggplot2 and sf. The coordinate reference system was changed from 4326 to 32611. The crsuggest package was used to find the appropriate CRS for the region. The base polygons were from the rnaturalearth package. After generating the map, the labels were modified in Inkscape.

I’ve really struggled with understanding the difference between geographic and projected coordinate systems. Geographic systems specify location using latitude and longitude. Longitude is specified in degrees in an East-West direction from the Prime Meridian whereas latitude is specified in degrees in a North-South direction of the equator. Together, the two coordinates specify a unique location on the Earth’s surface. The key point is that the latitude and longitude are anglular distances. Nearly all datasets are specified using the EPSG 4326 coordinate reference system.

Projected coordinate systems are based on a mathematical model that flattens the Earth’s surface and in the background an sf object’s units are being coverted from degrees to meters. This conversion means that specifying the limits of the bounding box must be adjusted for meters. The crsuggest package was used to find the appropriate CRS for the region. 32611 was chosen and it is a popular projected coordinate system across the world and is known as the Universal Transverse Mercator coordinate system. Wikipedia article here.

In the description, the crsuggest package states “Return the EPSG code or proj4string syntax for the top-ranking projected coordinate reference system returned by suggest_crs(). This function should be used with caution and is recommended for interactive work rather than in production data pipelines.”

Load Packages

library(colorspace)
library(crsuggest)
library(dplyr)
library(ggplot2)
library(ggspatial)
library(rnaturalearth)
library(rnaturalearthdata)
library(sf)

Create Layers

# world
world <- ne_countries(country = c("United States of America",
                                  "Colombia", "Cuba", "Jamaica", 
                                  "The Bahamas", "Cayman Islands"),
                      scale = 50) %>% 
  select(continent, subregion, adm0_a3, mapcolor7, geometry, label_x, label_y) %>% 
  sf::st_transform(crs = 32611)
# carribean
cb <- ne_countries(scale = 50) %>% 
  select(continent, subregion, adm0_a3, mapcolor7, geometry, label_x, label_y) %>% 
  filter(continent %in% c("North America")) %>% 
  filter(subregion == "Central America") %>% 
  mutate(mapcolor7 = factor(mapcolor7)) %>% 
  sf::st_transform(crs = 32611)
#cb labels
cb_labels <-
  cb %>% 
  select(adm0_a3, label_x, label_y) %>% 
  st_drop_geometry() %>% 
  mutate(geometry = st_as_sf(., coords = c("label_x", "label_y"), 
                           crs = 4326) %>% st_geometry()) %>% 
  st_as_sf() %>% 
  sf::st_transform(crs = 32611)
# get dimensions
st_bbox(cb)
# get best crs
crs <- suggest_top_crs(cb)

Plot Map

# plot
ggplot() +
geom_sf(data = world) +
geom_sf(data = cb, mapping = aes(fill = mapcolor7), show.legend = F) +
geom_sf_label(data = cb_labels, mapping = aes(label = adm0_a3)) +
coord_sf(expand = TRUE, 
         crs = crs,
         xlim = c(5.5e6, .65e6),
         ylim = c(1e6, 3.5e6)
) +
scale_fill_discrete_qualitative(palette = "Pastel 1") +
theme_bw() +
annotation_scale(
  bar_cols = c("black", "white"),
  line_width = 1,
  height = unit(0.1, "cm"),
  pad_x = unit(0.25, "cm"),
  pad_y = unit(0.25, "cm"),
  ) +
annotation_north_arrow(
  style = north_arrow_minimal(),
  height = unit(.5, "cm"), 
  width = unit(.5, "cm"),
  pad_x = unit(2, "cm"),
  pad_y = unit(.5, "cm")
  ) +
labs(title = "Central America", x = "", y = "")

Save Plot

ggsave("~/Desktop/a.pdf", height = 5, width = 8, units = "in")

Conclusion

This blog post showed how to create a map of Central America using ggplot2, sf and the crsuggest package.

Acknowledgements

This blog post was made possible thanks to:

  • Kyle Walker, the author of the crsuggest package. Find the repo here

  • To understand the impact of the 4326 CRS on area distortion, visit this awesome site: https://www.thetruesize.com

  • Vox explainer on Mercator projections

References

[1]
R Core Team, R: A language and environment for statistical computing. Vienna, Austria: R Foundation for Statistical Computing, 2024 [Online]. Available: https://www.R-project.org/
[2]
Y. Xie, C. Dervieux, and A. Presmanes Hill, Blogdown: Create blogs and websites with r markdown. 2024 [Online]. Available: https://github.com/rstudio/blogdown
[3]
K. Walker, Crsuggest: Obtain suggested coordinate reference system information for spatial data. 2022 [Online]. Available: https://CRAN.R-project.org/package=crsuggest
[4]
H. Wickham et al., ggplot2: Create elegant data visualisations using the grammar of graphics. 2024 [Online]. Available: https://ggplot2.tidyverse.org
[5]
D. Dunnington, Ggspatial: Spatial data framework for ggplot2. 2023 [Online]. Available: https://paleolimbot.github.io/ggspatial/
[6]
P. Massicotte and A. South, Rnaturalearth: World map data from natural earth. 2023 [Online]. Available: https://docs.ropensci.org/rnaturalearth/
[7]
E. Pebesma, Sf: Simple features for r. 2024 [Online]. Available: https://r-spatial.github.io/sf/

Disclaimer

The views, analysis and conclusions presented within this paper represent the author’s alone and not of any other person, organization or government entity. While I have made every reasonable effort to ensure that the information in this article was correct, it will nonetheless contain errors, inaccuracies and inconsistencies. It is a working paper subject to revision without notice as additional information becomes available. Any liability is disclaimed as to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause. The author(s) received no financial support for the research, authorship, and/or publication of this article.

Reproducibility

─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.0 (2024-04-24)
 os       macOS Sonoma 14.4
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2024-12-05
 pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version  date (UTC) lib source
 blogdown    * 1.19     2024-02-01 [1] CRAN (R 4.4.0)
 bookdown      0.41     2024-10-16 [1] CRAN (R 4.4.1)
 bslib         0.8.0    2024-07-29 [1] CRAN (R 4.4.0)
 cachem        1.1.0    2024-05-16 [1] CRAN (R 4.4.0)
 cli           3.6.3    2024-06-21 [1] CRAN (R 4.4.0)
 codetools     0.2-20   2024-03-31 [2] CRAN (R 4.4.0)
 colorspace    2.1-1    2024-07-26 [1] CRAN (R 4.4.0)
 devtools    * 2.4.5    2022-10-11 [1] CRAN (R 4.4.0)
 digest        0.6.37   2024-08-19 [1] CRAN (R 4.4.1)
 ellipsis      0.3.2    2021-04-29 [1] CRAN (R 4.4.0)
 evaluate      1.0.1    2024-10-10 [1] CRAN (R 4.4.1)
 fansi         1.0.6    2023-12-08 [1] CRAN (R 4.4.0)
 farver        2.1.2    2024-05-13 [1] CRAN (R 4.4.0)
 fastmap       1.2.0    2024-05-15 [1] CRAN (R 4.4.0)
 fs            1.6.5    2024-10-30 [1] CRAN (R 4.4.1)
 ggplot2     * 3.5.1    2024-04-23 [1] CRAN (R 4.4.0)
 ggthemes    * 5.1.0    2024-02-10 [1] CRAN (R 4.4.0)
 glue          1.8.0    2024-09-30 [1] CRAN (R 4.4.1)
 gtable        0.3.6    2024-10-25 [1] CRAN (R 4.4.1)
 htmltools     0.5.8.1  2024-04-04 [1] CRAN (R 4.4.0)
 htmlwidgets   1.6.4    2023-12-06 [1] CRAN (R 4.4.0)
 httpuv        1.6.15   2024-03-26 [1] CRAN (R 4.4.0)
 jquerylib     0.1.4    2021-04-26 [1] CRAN (R 4.4.0)
 jsonlite      1.8.9    2024-09-20 [1] CRAN (R 4.4.1)
 knitr         1.49     2024-11-08 [1] CRAN (R 4.4.1)
 labeling      0.4.3    2023-08-29 [1] CRAN (R 4.4.0)
 later         1.4.1    2024-11-27 [1] CRAN (R 4.4.1)
 lifecycle     1.0.4    2023-11-07 [1] CRAN (R 4.4.0)
 magrittr      2.0.3    2022-03-30 [1] CRAN (R 4.4.0)
 memoise       2.0.1    2021-11-26 [1] CRAN (R 4.4.0)
 mime          0.12     2021-09-28 [1] CRAN (R 4.4.0)
 miniUI        0.1.1.1  2018-05-18 [1] CRAN (R 4.4.0)
 munsell       0.5.1    2024-04-01 [1] CRAN (R 4.4.0)
 pillar        1.9.0    2023-03-22 [1] CRAN (R 4.4.0)
 pkgbuild      1.4.5    2024-10-28 [1] CRAN (R 4.4.1)
 pkgconfig     2.0.3    2019-09-22 [1] CRAN (R 4.4.0)
 pkgload       1.4.0    2024-06-28 [1] CRAN (R 4.4.0)
 profvis       0.4.0    2024-09-20 [1] CRAN (R 4.4.1)
 promises      1.3.2    2024-11-28 [1] CRAN (R 4.4.1)
 purrr         1.0.2    2023-08-10 [1] CRAN (R 4.4.0)
 R6            2.5.1    2021-08-19 [1] CRAN (R 4.4.0)
 Rcpp          1.0.13-1 2024-11-02 [1] CRAN (R 4.4.1)
 remotes       2.5.0    2024-03-17 [1] CRAN (R 4.4.0)
 rlang         1.1.4    2024-06-04 [1] CRAN (R 4.4.0)
 rmarkdown     2.29     2024-11-04 [1] CRAN (R 4.4.1)
 rstudioapi    0.17.1   2024-10-22 [1] CRAN (R 4.4.1)
 sass          0.4.9    2024-03-15 [1] CRAN (R 4.4.0)
 scales        1.3.0    2023-11-28 [1] CRAN (R 4.4.0)
 sessioninfo   1.2.2    2021-12-06 [1] CRAN (R 4.4.0)
 shiny         1.9.1    2024-08-01 [1] CRAN (R 4.4.0)
 stringi       1.8.4    2024-05-06 [1] CRAN (R 4.4.0)
 stringr       1.5.1    2023-11-14 [1] CRAN (R 4.4.0)
 tibble        3.2.1    2023-03-20 [1] CRAN (R 4.4.0)
 urlchecker    1.0.1    2021-11-30 [1] CRAN (R 4.4.0)
 usethis     * 3.1.0    2024-11-26 [1] CRAN (R 4.4.1)
 utf8          1.2.4    2023-10-22 [1] CRAN (R 4.4.0)
 vctrs         0.6.5    2023-12-01 [1] CRAN (R 4.4.0)
 withr         3.0.2    2024-10-28 [1] CRAN (R 4.4.1)
 xfun          0.49     2024-10-31 [1] CRAN (R 4.4.1)
 xtable        1.8-4    2019-04-21 [1] CRAN (R 4.4.0)
 yaml          2.3.10   2024-07-26 [1] CRAN (R 4.4.0)

 [1] /Users/rkw/Library/R/arm64/4.4/library
 [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────