Difference between revisions of "Creating Hexbin Maps in R"
Line 7: | Line 7: | ||
== Why Hexagons? == |
== Why Hexagons? == |
||
+ | Regularly shaped grids are often used to normalize geography for mapping in instances where polygons are irregularly shaped (i.e., political boundaries). A hexagon grid is an alternative to the square (fishnet) grid typically used in GIS analysis and thematic mapping. Aggregating data into hexagons is advantageous as the edge effects of the grid shape reduce sampling bias. In addition, hexagons can be used to obscure sensitive source data (e.g., personal addresses). |
||
== Getting Started == |
== Getting Started == |
||
Line 68: | Line 69: | ||
=== Add Map Elements === |
=== Add Map Elements === |
||
+ | |||
+ | Labels can be added to our map to provide viewers with geographic reference information. This is especially important To add labels to the hexbin map, |
||
== Conclusion == |
== Conclusion == |
Revision as of 10:22, 8 December 2021
Contents
Objective
The objective of this tutorial is to create a hexbin choropleth map of U.S. education costs in RStudio. Users will learn how to create a hexbin map from a geospatial object and plot thematic openly available data.This tutorial will additionally demonstrate how to add cartographic elements including symbolization and labelling. Through this tutorial, users will have the opportunity to explore the spatial capabilities of the software and learn more about advanced cartographic design.
This tutorial contributes to the collection of Open Source GIS tutorials created by Carleton University students.
'Note: This tutorial assumes basic knowledge of the R programming language. This version of the tutorial was created using a Windows platform with R version.'
Why Hexagons?
Regularly shaped grids are often used to normalize geography for mapping in instances where polygons are irregularly shaped (i.e., political boundaries). A hexagon grid is an alternative to the square (fishnet) grid typically used in GIS analysis and thematic mapping. Aggregating data into hexagons is advantageous as the edge effects of the grid shape reduce sampling bias. In addition, hexagons can be used to obscure sensitive source data (e.g., personal addresses).
Getting Started
Downloading the Software
The first step of this tutorial is downloading R and RStudio if they are not already installed on your device. R version 4.1.2 is the latest version of the software released in 2021.
R is a widely used open-source software environment used for data manipulation and analysis (statistics, graphics, etc.). R is easily customizable and is executed line by line in a console. For the purposes of this tutorial, R will be accessed through an integrated development environment (IDE) called RStudio.
Finding Data
Spatial data
When creating a hexagonal map, users have the option to create a hexbin map from (1) a geospatial object or (2) a list of coordinates. For the purposes of this tutorial, an existing hexagon boundary file (.geojson) of the United States will be used. Download the data in .geojson format and save to a new project folder.
The hexgrid is available to download HERE.
Non-spatial data
The statistical data for this tutorial will be sourced from the United States (U.S.) National Science Board. The data of interest is the state-level “Average Undergraduate Charge at Public 4-Year Institutions” from 1994 to 2019. The charge includes the tuition, required fees, room, and board for a full-time undergraduate student who is a state resident. This data serves as a useful indicator of the accessibility of higher education.
The data is available to view and download HERE.
To start a new R script, click the icon in the top left corner of the script window or by clicking through the top menu (File > New > R script).
Next, save the R script to the folder that contains your data from the step above. Click the 'save' icon in the script window or (File > Save As).
Set your working directory at the start of your new session. This is the folder where R reads and saves files. This can be accessed through the top menu (Session > Set Working Directory > Choose Directory) or by writing a command:
setwd("~/FALL 2021/GEOM 4008/Data")
Creating a Choropleth Map
Installing Packages
This tutorial requires a number of packages (see list below). These packages can be installed using the top menu (Tools > Install Packages).
Tip: If you want to learn more about a specific package or function, you can write a command in the R console to the view the corresponding 'Help' page.
?mutate
Importing Data
Before reading your data into R, ensure the files are located in the folder you set as your directory.
To import the hexbin data, we will use the geojson_read()
function. After, the data needs to be reformatted and fortified to create a data frame format.
hex <- geojson_read("us_states_hexgrid.geojson", what = "sp")
Reformat the 'google_name' field using the mutate
and gsub
functions. For example, Utah (United States) will be changed to Utah.
hex@data = hex@data %>% mutate(google_name = gsub(" \\(United States\\)", "", google_name))
Fortify the data using the tidy
function. This will produce a data frame output which is necessary to plot the data using the ggplot2
library.
hex_fortify <- tidy(hex, region = "google_name")
Join Spatial and Non-Spatial Data
Symbolization
Add Map Elements
Labels can be added to our map to provide viewers with geographic reference information. This is especially important To add labels to the hexbin map,
Conclusion
References
https://www.r-graph-gallery.com/328-hexbin-map-of-the-usa.html
https://team.carto.com/u/andrew/tables/andrew.us_states_hexgrid/public/map