Developing the Traffic Related Air Pollution and the Burden of Childhood Asthma in the Contiguous United States in 2000 and 2010 Visualizations
Prerequisites
- Intermediate level R
- Basic level HTML
- Basic understanding of how FIPS code is structured
- Basic understanding of how shapefiles are structured
In this tutorial, we will go step by step on how to build the interactive map presented in the burden of asthma due to TRAP study using R. See the maps and associated data sets in the inserts below.
Step 1: Load the required libraries
Step 2: Set the working directory to the required folder.
In this step we will set the working directory to the location of the files we downloaded, we will also use this directory to store the final interactive map as an HTML file.
Step 3: Read the dataset
Step 4: Exploring the data sets
"county_wide" is a data frame with 18,654 rows and 8 columns, each row represents a county. 'GEOID' is the unique identifier for each county, it's taken from the FIPS code in which the first two digits represent the state and the following three digits represent the county. 'Pollutant' is a factor with three levels (NO2, PM10, and PM2.5) we will only be working with 'NO2' in this tutorial (in the next step we will filter out the data). 'AF' is the percentage of childhood asthma incident cases due to the pollutant measured at the county level, we will be mapping 'AF' to the interactive map as our main variable of interest. 'year' is a factor with two levels (2000 and 2010), we will be working with data for the year 2010 only.
Step 5: Filter the "county_wide" data set
We only need rows with "NO2" as the "Pollutant" and rows with "2010" as the "year". Using the pipe operator "%>%" and the filter() function we filter the rows we need then save the filtered data set to a new dataset naming it "county_NO2". The filtered data set "county_NO2" now should contain 3,109 rows and 8 columns.
Step 6: Examining the "counties" shape file
By using the "@data" we can access the data portion of a shapefile. The "counties" shapefile has 3,109 rows and 17 columns. The "GEOID" column is the unique identifier for each county.
Step 7: Merge the data set "County_NO2" with the Shapefile "counties"
Step 8: Creating color palettes
We will manually build a color palette, the colors will be mapped to the distribution of our main variable "AF - the percentage of childhood asthma incident cases due to NO2". We will be using four colors to map the distribution of "AF" using Hex color codes (you can use google to look up the colors i.e. #ffe950), the colors are green, yellow, red and black. The previewColors() function allows you to examine the distribution of colors, in this case, we want to see how are colors are distributed for values between 0 and 55. The color palette is saved to "rampcols". We then use the colorNumeric() function with the color palette we created to assign color values to our main variable "AF", however, here we actually want to assign the values to the percentage so we multiply "AF" with 100. The assigned color values are stored in a variable named "AF_pal"
Step 9: Creating labels
In this step we will be creating three things: "labels" that will present county data when we hover the mouse over the map "label_legend" the legend for the map "label_Title" the title of the map The code will allow us to observe specific data when we hover the mouse over a county, the data presented will be the county names "NAME" and the state abbreviation "state_name" separated by and ",", the population of children "total_children", the attributable number of cases "AC", the percentage of childhood asthma cases due to NO2 "AF" and the average NO2 concentration for the county "conc". The sprintf() function allows us to combine formatted text and variable values. while the HTML() function from "htmltools" package marks a given text as HTML format.
Step 10: Creating the interactive map
Using the leaflet() function from the "leaflet" package, we put together a map using the previous data sets, color palettes and labels we created. The map is assigned to a variable "interactiveMap" as a leaflet object.
Step 11: Saving the map as an HTML file
Using the saveWidget() function from the "htmlwidgets" package we can transform our leaflet object "interactiveMap" into an HTML object and save it to our working directory. The final map is presented below. Percentage of childhood asthma incident cases due to NO2 from all causes in the year 2010.
FILES (3)
-
county_wide.Rda
Size: 487 KB
Last Modified: Wed Sep 12 2018Data set with information from the burden of childhood asthma due to TRAP study aggregated at the county level.
-
counties_shp.Rda
Size: 1 MB
Last Modified: Mon Sep 17 2018County-level shapefile for the US
-
Leaflet_Map_tutorial.R
Size: 3 KB
Last Modified: Tue Oct 02 2018R script containing the code to produce an interactive map