Fine particulate matter (PM2.5) is an ambient air pollutant for which there is strong evidence that it is harmful to human health. In the United States, the Environmental Protection Agency (EPA) is tasked with setting national ambient air quality standards for fine PM and for tracking the emissions of this pollutant into the atmosphere. Approximatly every 3 years, the EPA releases its database on emissions of PM2.5. This database is known as the National Emissions Inventory (NEI). You can read more information about the NEI at the EPA National Emissions Inventory web site.
http://www.epa.gov/ttn/chief/eiinformation.html
For each year and for each type of PM source, the NEI records how many tons of PM2.5 were emitted from that source over the course of the entire year. The data that you will use for this assignment are for 1999, 2002, 2005, and 2008.
The data for this assignment are available from the course web site as a single zip file:
https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip
The zip file contains two files:
PM2.5 Emissions Data: This file contains a data frame with all of the PM2.5 emissions data for 1999, 2002, 2005, and 2008. For each year, the table contains number of tons of PM2.5 emitted from a specific type of source for the entire year. It’s columns are as follows:
fips: A five-digit number (represented as a string) indicating the U.S. county SCC: The name of the source as indicated by a digit string (see source code classification table) Pollutant: A string indicating the pollutant Emissions: Amount of PM2.5 emitted, in tons type: The type of source (point, non-point, on-road, or non-road) year: The year of emissions recorded
Source Classification Code Table: This table provides a mapping from the SCC digit strings in the Emissions table to the actual name of the PM2.5 source. The sources are categorized in a few different ways from more general to more specific and you may choose to explore whatever categories you think are most useful. For example, source “10100101” is known as “Ext Comb /Electric Gen /Anthracite Coal /Pulverized Coal”.
The overall goal of this assignment is to explore the National Emissions Inventory database and see what it say about fine particulate matter pollution in the United states over the 10-year period 1999–2008. There are 6 questions to be answered:
1: Have total emissions from PM2.5 decreased in the United States from 1999 to 2008? Using the base plotting system, make a plot showing the total PM2.5 emission from all sources for each of the years 1999, 2002, 2005, and 2008.
2: Have total emissions from PM2.5 decreased in the Baltimore City, Maryland (fips==“24510”) from 1999 to 2008? Use the base plotting system to make a plot answering this question.
3: Of the four types of sources indicated by the type (point, nonpoint, onroad, nonroad) variable, which of these four sources have seen decreases in emissions from 1999–2008 for Baltimore City? Which have seen increases in emissions from 1999–2008? Use the ggplot2 plotting system to make a plot answer this question.
4: Across the United States, how have emissions from coal combustion-related sources changed from 1999–2008?
5: How have emissions from motor vehicle sources changed from 1999–2008 in Baltimore City?
6: Compare emissions from motor vehicle sources in Baltimore City with emissions from motor vehicle sources in Los Angeles County, California (fips==“06037”). Which city has seen greater changes over time in motor vehicle emissions?
Session Info for reproducibility:
sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 17763)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] compiler_3.6.1 magrittr_1.5 tools_3.6.1 htmltools_0.4.0
## [5] yaml_2.2.0 Rcpp_1.0.3 stringi_1.4.4 rmarkdown_2.1
## [9] knitr_1.27 stringr_1.4.0 xfun_0.12 digest_0.6.23
## [13] rlang_0.4.4 evaluate_0.14
Before we begin addressing each question we must first download and prepare the data, as well as load any packages we plan to use.
# Check to see if the file has already been downloaded. If not, do so.
if(!(file.exists("summarySCC_PM25.rds") && file.exists("Source_Classification_Code.rds"))) {
file <- "NEI_data.zip"
if(!file.exists(file)) {
URL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip"
download.file(url = URL, destfile = file, method = "curl")
}
unzip(file)
}
This extracted two new files for us to read into objects, which I will do now.
SCC <- readRDS("Source_Classification_Code.rds")
SummarySCC <- readRDS("summarySCC_PM25.rds")
# Viewing the structures and first few lines of each file...
str(SCC)
## 'data.frame': 11717 obs. of 15 variables:
## $ SCC : Factor w/ 11717 levels "10100101","10100102",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ Data.Category : Factor w/ 6 levels "Biogenic","Event",..: 6 6 6 6 6 6 6 6 6 6 ...
## $ Short.Name : Factor w/ 11238 levels "","2,4-D Salts and Esters Prod /Process Vents, 2,4-D Recovery: Filtration",..: 3283 3284 3293 3291 3290 3294 3295 3296 3292 3289 ...
## $ EI.Sector : Factor w/ 59 levels "Agriculture - Crops & Livestock Dust",..: 18 18 18 18 18 18 18 18 18 18 ...
## $ Option.Group : Factor w/ 25 levels "","C/I Kerosene",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Option.Set : Factor w/ 18 levels "","A","B","B1A",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ SCC.Level.One : Factor w/ 17 levels "Brick Kilns",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ SCC.Level.Two : Factor w/ 146 levels "","Agricultural Chemicals Production",..: 32 32 32 32 32 32 32 32 32 32 ...
## $ SCC.Level.Three : Factor w/ 1061 levels "","100% Biosolids (e.g., sewage sludge, manure, mixtures of these matls)",..: 88 88 156 156 156 156 156 156 156 156 ...
## $ SCC.Level.Four : Factor w/ 6084 levels "","(NH4)2 SO4 Acid Bath System and Evaporator",..: 4455 5583 4466 4458 1341 5246 5584 5983 4461 776 ...
## $ Map.To : num NA NA NA NA NA NA NA NA NA NA ...
## $ Last.Inventory.Year: int NA NA NA NA NA NA NA NA NA NA ...
## $ Created_Date : Factor w/ 57 levels "","1/27/2000 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Revised_Date : Factor w/ 44 levels "","1/27/2000 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Usage.Notes : Factor w/ 21 levels ""," ","includes bleaching towers, washer hoods, filtrate tanks, vacuum pump exhausts",..: 1 1 1 1 1 1 1 1 1 1 ...
head(SCC)
## SCC Data.Category
## 1 10100101 Point
## 2 10100102 Point
## 3 10100201 Point
## 4 10100202 Point
## 5 10100203 Point
## 6 10100204 Point
## Short.Name
## 1 Ext Comb /Electric Gen /Anthracite Coal /Pulverized Coal
## 2 Ext Comb /Electric Gen /Anthracite Coal /Traveling Grate (Overfeed) Stoker
## 3 Ext Comb /Electric Gen /Bituminous Coal /Pulverized Coal: Wet Bottom
## 4 Ext Comb /Electric Gen /Bituminous Coal /Pulverized Coal: Dry Bottom
## 5 Ext Comb /Electric Gen /Bituminous Coal /Cyclone Furnace
## 6 Ext Comb /Electric Gen /Bituminous Coal /Spreader Stoker
## EI.Sector Option.Group Option.Set
## 1 Fuel Comb - Electric Generation - Coal
## 2 Fuel Comb - Electric Generation - Coal
## 3 Fuel Comb - Electric Generation - Coal
## 4 Fuel Comb - Electric Generation - Coal
## 5 Fuel Comb - Electric Generation - Coal
## 6 Fuel Comb - Electric Generation - Coal
## SCC.Level.One SCC.Level.Two SCC.Level.Three
## 1 External Combustion Boilers Electric Generation Anthracite Coal
## 2 External Combustion Boilers Electric Generation Anthracite Coal
## 3 External Combustion Boilers Electric Generation Bituminous/Subbituminous Coal
## 4 External Combustion Boilers Electric Generation Bituminous/Subbituminous Coal
## 5 External Combustion Boilers Electric Generation Bituminous/Subbituminous Coal
## 6 External Combustion Boilers Electric Generation Bituminous/Subbituminous Coal
## SCC.Level.Four Map.To Last.Inventory.Year
## 1 Pulverized Coal NA NA
## 2 Traveling Grate (Overfeed) Stoker NA NA
## 3 Pulverized Coal: Wet Bottom (Bituminous Coal) NA NA
## 4 Pulverized Coal: Dry Bottom (Bituminous Coal) NA NA
## 5 Cyclone Furnace (Bituminous Coal) NA NA
## 6 Spreader Stoker (Bituminous Coal) NA NA
## Created_Date Revised_Date Usage.Notes
## 1
## 2
## 3
## 4
## 5
## 6
str(SummarySCC)
## 'data.frame': 6497651 obs. of 6 variables:
## $ fips : chr "09001" "09001" "09001" "09001" ...
## $ SCC : chr "10100401" "10100404" "10100501" "10200401" ...
## $ Pollutant: chr "PM25-PRI" "PM25-PRI" "PM25-PRI" "PM25-PRI" ...
## $ Emissions: num 15.714 234.178 0.128 2.036 0.388 ...
## $ type : chr "POINT" "POINT" "POINT" "POINT" ...
## $ year : int 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 ...
head(SummarySCC)
## fips SCC Pollutant Emissions type year
## 4 09001 10100401 PM25-PRI 15.714 POINT 1999
## 8 09001 10100404 PM25-PRI 234.178 POINT 1999
## 12 09001 10100501 PM25-PRI 0.128 POINT 1999
## 16 09001 10200401 PM25-PRI 2.036 POINT 1999
## 20 09001 10200504 PM25-PRI 0.388 POINT 1999
## 24 09001 10200602 PM25-PRI 1.490 POINT 1999
# Lastly, I'll load in a couple useful R packages
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.2
library(plyr)
## Warning: package 'plyr' was built under R version 3.6.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
We can see from the structure of SummarySCC that flips, SCC, Pollutant, type, and year are not factors when they need to be for further analysis. We can convert them to factors by subsetting to those columns and using lapply.
columns <- c("fips", "SCC", "Pollutant", "type", "year")
SummarySCC[,columns] <- lapply(SummarySCC[,columns], factor)
# Fixing some NA issues with formatting
levels(SummarySCC$fips)[1] = NA
SummarySCC <- SummarySCC[complete.cases(SummarySCC),]
Recall Q1: Have total emissions from PM2.5 decreased in the United States from 1999 to 2008? Using the base plotting system, make a plot showing the total PM2.5 emission from all sources for each of the years 1999, 2002, 2005, and 2008.
This question is straightforward to answer. We simply need to aggregate the total emmissions by year, then apply a barplot.
totalemissions <- aggregate(Emissions ~ year, SummarySCC, sum)
print(totalemissions)
## year Emissions
## 1 1999 7332967
## 2 2002 5635780
## 3 2005 5454703
## 4 2008 3456273
barplot(
(totalemissions$Emissions)/10^6,
names.arg = totalemissions$year,
xlab = "Year",
ylab = "PM2.5 Emissions (10^6 Tons)",
main = "Total PM2.5 Emissions From All US Sources"
)
We see from the summary and from the barplot that emissions have decreased over time.
Recall Q2: Have total emissions from PM2.5 decreased in the Baltimore City, Maryland (fips==“24510”) from 1999 to 2008? Use the base plotting system to make a plot answering this question.
This question is just a further subset of the previous question and can be handled much the same way.
baltimore <- subset(SummarySCC, fips == "24510")
baltimoreEmissions <- aggregate(Emissions ~ year, baltimore, sum)
print(baltimoreEmissions)
## year Emissions
## 1 1999 3274.180
## 2 2002 2453.916
## 3 2005 3091.354
## 4 2008 1862.282
barplot(
(baltimoreEmissions$Emissions)/100,
names.arg = baltimoreEmissions$year,
xlab = "Year",
ylab = "PM2.5 Emissions (100 Tons)",
main = "Total PM2.5 Emissions From Baltimore"
)
We see from the chart that emissions in Baltimore haven’t decreased every year, but the trend has been slightly downward.
Recall Q3: Of the four types of sources indicated by the type (point, nonpoint, onroad, nonroad) variable, which of these four sources have seen decreases in emissions from 1999–2008 for Baltimore City? Which have seen increases in emissions from 1999–2008? Use the ggplot2 plotting system to make a plot answer this question.
We can use ggplot and the facets geom to create an individual plot for each of the four types. We can also apply a linear regression to each to capture a trend.
g <- ggplot(aes(x = year, y = Emissions, fill = type), data = baltimore)
g + geom_bar(stat="identity") +
facet_grid(.~type) +
labs(x = "year", y = expression("Total PM"[2.5]*" Emission (Tons)")) +
labs(title = expression("PM"[2.5]*" Emissions, Baltimore City 1999-2008 by Source Type")) +
guides(fill=FALSE) +
geom_smooth(method = "lm")
We can see from the plots that non-road, non-point, and on-road have generally decreased. Point is unclear as it has increased until 2005, but a big drop in 2008.
Recall Q4: Across the United States, how have emissions from coal combustion-related sources changed from 1999–2008?
To answer this, first we have to understand which fields count as coal combustion-related. For this, we look at our as-of-yet unused data frame, SCC. We want to subset SCC Level One contains the string “comb” (for combustion) and Level Four contains the string “coal”. This is the best guess.
We can use the grepl function to find “comb” and “coal” within the subsets.
## First we do some cleaning. gsub will remove the first elements from the second.
names(SCC) <- gsub("\\.","",names(SCC))
##Now we create new subsets
Combustion <- grepl(pattern = "comb", SCC$SCCLevelOne, ignore.case = TRUE)
Coal<-grepl(pattern = "coal", SCC$SCCLevelFour, ignore.case = TRUE)
CombustionSCC <- SCC[Combustion & Coal,]$SCC
CombustionValues <- SummarySCC[SummarySCC$SCC %in% CombustionSCC,]
TotalCombEmByYr <- aggregate(Emissions ~ year, CombustionValues, sum)
head(TotalCombEmByYr)
## year Emissions
## 1 1999 551535.2
## 2 2002 474708.6
## 3 2005 481834.4
## 4 2008 324999.1
## Now we can plot around the last object, TotalCombEmByYr
g <- ggplot(aes(x = year,y = Emissions/10^5, fill = year), data = TotalCombEmByYr)
g + geom_bar(stat = "identity", width = 0.75) +
scale_fill_manual(values = c("red","blue","green", "orange")) +
guides(fill = FALSE) +
labs(x = "year", y = expression("Total PM"[2.5]*" Emission (10^5 Tons)")) +
labs(title = expression("PM"[2.5]*" Coal Combustion Source Emissions Across US from 1999-2008"))
We can see from the chart that the emissions from coal combustion are generally decreasing. I added colors to the bars just for fun and for practice doing so.
Recall Q5: How have emissions from motor vehicle sources changed from 1999–2008 in Baltimore City?
Simlar to the prior question, we first have to subset on motor vehicles, which we can assume is anything containing “vehicle” in the ElSector column of SCC.
vehicle <- grepl(pattern = "vehicle", SCC$EISector, ignore.case = TRUE)
vehicleSCC <- SCC[vehicle,]$SCC
SummarySCCVehicle <- SummarySCC[SummarySCC$SCC %in% vehicleSCC,]
vehicleBaltimore <- subset(SummarySCCVehicle, fips == "24510")
vehicleBaltEm <- aggregate(Emissions ~ year, vehicleBaltimore, sum)
## Now we plot
g <- ggplot(aes(x = year,y = Emissions/10^5, fill = year), data = vehicleBaltEm)
g + geom_bar(stat = "identity", width = 0.75) +
scale_fill_manual(values = c("red","blue","green", "orange")) +
guides(fill = FALSE) +
labs(x = "year", y = expression("Total PM"[2.5]*" Emission (10^5 Tons)")) +
labs(title = expression("PM"[2.5]*" Balitmore Vehicle Emissions from 1999-2008"))
We see from the bar plot that motor vehicle emissions have generally decreased in Baltimore.
Recall Q6: Compare emissions from motor vehicle sources in Baltimore City with emissions from motor vehicle sources in Los Angeles County, California (fips==“06037”). Which city has seen greater changes over time in motor vehicle emissions?
We can answer this question by subsetting our vehicle table, SummarySCCVehicle, twice. Once for Baltimore and once for Los Angeles County. Then we can rbind the two tables together. Then we can use the facet function in ggplot to see the cities side by side.
BaltimoreSubTable <- subset(SummarySCCVehicle, fips == "24510") ## We already made this object earlier, but again doesn't hurt
LACountySubTable <- subset(SummarySCCVehicle, fips == "06037")
# Adding a "City" column to these sub tables
BaltimoreSubTable$City <- "Baltimore"
LACountySubTable$City <- "Los Angeles County"
# Combining
BothCities <- rbind(BaltimoreSubTable, LACountySubTable)
## Now we plot
g <- ggplot(aes(x = year, y = Emissions, fill = City), data = BothCities)
g + geom_bar(stat = "identity", width = 0.75) +
scale_fill_manual(values = c("skyblue","royalblue","blue", "navy")) +
facet_grid(.~City) +
guides(fill = FALSE) +
labs(x = "year", y = expression("Total PM"[2.5]*" Emission")) +
labs(title = expression("PM"[2.5]*" Balitmore & LA County Vehicle Emissions from 1999-2008"))