Make a map with R and ggplot2

Learn how to…

  • Open and save project files using RStudio

  • Read vector data files (e.g. shapefile or GeoJSON) using the sf package

  • Filter data by attribute using the {dplyr} package

  • Create a map with the {ggplot2} package using an attribute to define the aesthetics of the map

  • Label and export a map using the {ggplot2} package


Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE

Step-by-step instructions

Complete all of the following steps using the linked function documentation for reference:

  1. Open the 02_map-R.RProj project file (see using RStudio Projects) and open the RMarkdown template found within the project folder.

  2. Read any additional data using sf::read_sf() or sf::st_read()

  3. Filter data by attribute using dplyr::filter()

  4. Create a plot with ggplot2::ggplot() and a geom with ggplot2::geom_sf()

  5. Specify one or more aesthetic attributes using ggplot2::aes()

  6. Add an appropriate scale for the attribute using ggplot2::scale_fill_brewer(), ggplot2::scale_color_brewer(), or another ggplot2 scale function

  7. Set a theme using ggplot2::theme_minimal() or another theme function

  8. Customize the map title, caption, and legend using ggplot2::labs(). Depending on the attribute type, you may want to use a labelling function from the {scales} package, e.g. label_dolar() for currency values or label_percent() for percentages. These functions are passed to the labels parameter of your scale function.

  9. Export the map as a PDF file using ggplot2::ggsave()

Optional bonus activities

Create one or more additional maps using any of the following approaches for an extra bonus points:

  • Create a multi-panel map using a faceting function. Typically, multi-panel maps are used for categorical variables or binned continuous variables. Consider using the dplyr::mutate() function to re-code a categorical variable or using a binned scale to convert a continuous variable into a discrete variable.

  • Create a bar chart showing the distribution of the mapped attribute by location or a scatter plot to compare the mapped attribute data to another attribute in your data. Ch. 3 Data visualization in Wickham and Grolemund (2022) has examples of both types of data visualizations or you can explore the documentation for ggplot2::geom_bar() or ggplot2::geom_point().

Submitting the completed assignment

Add the PDF map and a RMarkdown file with a brief description of your process and the map to the assignment folder in your course GitHub repository. If your map uses data that is not included in the Natural Earth Quick Start file, you must include links to sources where the data can be downloaded to allow the project file to be opened for review and feedback. If data files are less than 50 MB, please add the files to the GitHub repository in the same folder as the project file.

References

Wickham, Hadley, and Garrett Grolemund. 2022. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. 2nd edition (WIP). Sebastopol, CA: O’Reilly Media. https://r4ds.had.co.nz.