• Introduction
  • Choropleth map
  • The 3D map
  • References

Introduction

Here I make some choropleth maps and 3D map using R functionalities. I will start by getting the shapefile of Kenya from GADM for all counties in Kenya, that is, level = 1.

kenya <- raster::getData('GADM', country = 'KEN', level = 1) 

I then change the shapefile to sf object.

kenya_sf <- kenya %>% st_as_sf()

I then look for climate data which I get from worldclim database.

clim <- raster::getData('worldclim', var = 'tmin', res = 10)

The next step is to crop and mask the world raster to the boundary of Kenya. In this case, we take the clim data and pass it to crop function and them mask, both from raster package.

clim_masked <- clim %>% 
  raster::crop(kenya_sf) %>% 
  raster::mask(kenya_sf)

Since the clim_masked layer has got 12 layers, I want to extract only the 7th layer which corresponds to the month of July. I then set the extracted matrix as a dataframe them rename the column to July_tmin.

clim_july <- clim_masked[[7]] %>% 
  raster::extract(kenya_sf, fun = mean) %>% 
  as.data.frame() %>% 
  mutate(July_tmin = V1/10) %>% 
  dplyr::select(July_tmin)

The next step is to add this extracted data of July_tmin to the kenya_sf object as a column so that we can use it in subsequent mapping.

kenya_sf_mutate <- kenya_sf %>% mutate(clim_july)

The next step is to transform the crs of the kenya_sf_mutate from WGS 84 to EPSG 21097 which is a projected crs hence the units will be in metres on map.

kenya_sf_transformed <- kenya_sf_mutate %>% st_transform(21097)

Choropleth map

Now I build the map.

mf_init(kenya_sf_transformed, theme = 'iceberg')
mf_shadow(kenya_sf_transformed, col = 'purple', cex = 2)
mf_map(kenya_sf_transformed, add = TRUE)
mf_map(kenya_sf_transformed, var = 'July_tmin', type = 'choro', pal = 'Greens', add = TRUE)
mf_layout(title = 'Kenya Counties Temperature',
          credits = paste0("Data source: GADM and worldclim ", 'mapsf ',
                           packageVersion('mapsf')))

The 3D map

Lastly, I make a 3D map.

gg_shp <- ggplot(data = kenya_sf_transformed) +
  geom_sf(aes(fill = July_tmin)) +
  scale_fill_viridis() +
  ggtitle('Minimum temperature Kenya') +
  theme_bw()
plot_gg(gg_shp, multicore = TRUE, width = 5, height = 5, 
        scale = 200, windowsize = c(1280, 720), zoom = 0.65, 
        phi = 50, sunangle = 60, theta = 45)

Note, I had to generate the plot in the console then add it to the rmarkdown via below code.

Amazing 3D plot in R

References

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, Winston Chang, and Richard Iannone. 2021. Rmarkdown: Dynamic Documents for r. https://CRAN.R-project.org/package=rmarkdown.
Bivand, Roger S., Edzer Pebesma, and Virgilio Gomez-Rubio. 2013. Applied Spatial Data Analysis with R, Second Edition. Springer, NY. https://asdar-book.org/.
Garnier, Simon. 2021a. Viridis: Colorblind-Friendly Color Maps for r. https://CRAN.R-project.org/package=viridis.
———. 2021b. viridisLite: Colorblind-Friendly Color Maps (Lite Version). https://CRAN.R-project.org/package=viridisLite.
Giraud, Timothée. 2022. Mapsf: Thematic Cartography. https://CRAN.R-project.org/package=mapsf.
Henry, Lionel, and Hadley Wickham. 2020. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Hijmans, Robert J. 2021. Raster: Geographic Data Analysis and Modeling. https://rspatial.org/raster.
Morgan-Wall, Tyler. 2021. Rayshader: Create Maps and Visualize Data in 2d and 3d. https://CRAN.R-project.org/package=rayshader.
Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
Ooms, Jeroen. 2021a. Av: Working with Audio and Video in r. https://CRAN.R-project.org/package=av.
———. 2021b. Magick: Advanced Graphics and Image-Processing in r. https://CRAN.R-project.org/package=magick.
Pebesma, Edzer. 2018. Simple Features for R: Standardized Support for Spatial Vector Data.” The R Journal 10 (1): 439–46. https://doi.org/10.32614/RJ-2018-009.
———. 2021. Sf: Simple Features for r. https://CRAN.R-project.org/package=sf.
Pebesma, Edzer J., and Roger S. Bivand. 2005. “Classes and Methods for Spatial Data in R.” R News 5 (2): 9–13. https://CRAN.R-project.org/doc/Rnews/.
Pebesma, Edzer, and Roger Bivand. 2021. Sp: Classes and Methods for Spatial Data. https://CRAN.R-project.org/package=sp.
R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
———. 2021b. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
———. 2021c. Tidyverse: Easily Install and Load the Tidyverse. https://CRAN.R-project.org/package=tidyverse.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2021. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2021. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.
———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.
———. 2021. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://yihui.org/knitr/.
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.