Setting up the defaults:
knitr::opts_chunk$set(echo = TRUE, results = "asis")
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.
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.
For each question:
The data for this assignment are available from the course web site as a single zip file:
The zip file contains two files:
PM2.5 Emissions Data (summarySCC_PM25.rds): 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. Here are the first few rows.
- 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
Source Classification Code Table (Source_Classification_Code.rds): 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”.
You can read each of the two files using the readRDS() function in R. For example, reading in each file can be done with the following code:
## This first line will likely take a few seconds. Be patient!
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")
as long as each of those files is in your current working directory (check by calling dir() and see if those files are in the listing).
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. You may use any R package you want to support your analysis.
You must address the following questions and tasks in your exploratory analysis. For each question/task you will need to make a single plot. Unless specified, you can use any plotting system in R to make your plot.
plot1 <- function(){
subNEI <- NEI[ , c("Emissions", "year")]
subNEI$year=as.factor(subNEI$year)
freqPerYear <- aggregate(Emissions~year,subNEI, FUN=sum)
subNEI <- NULL
## Save file and close device
#png(file = "plot1.png")
with(freqPerYear,barplot(Emissions, names=year, xlab = "Year", ylab = "PM2.5 (tons)", main = "PM2.5 Total Emissions in the United States"))
#dev.off()
}
plot1()
plot2 <- function(){
subNEI <- NEI[NEI$fips=="24510", c("Emissions", "year")]
subNEI$year=as.factor(subNEI$year)
freqPerYear <- aggregate(Emissions~year,subNEI, FUN=sum)
subNEI <- NULL
with(freqPerYear,barplot(Emissions, names = year, xlab = "Year", ylab = "PM2.5 (tons)", main = "PM2.5 Total Emissions in Baltimore City, Marland"))
## Save file and close device
#dev.copy(png,"plot2.png", width=480, height=480)
#dev.off()
}
plot2()
plot3 <- function(){
library("ggplot2")
subNEI <- NEI[NEI$fips=="24510", c("Emissions", "year", "type")]
subNEI$year <- as.factor(subNEI$year)
subNEI$type <- as.factor(subNEI$type)
freqPerYear <- aggregate(Emissions~year + type, subNEI, FUN=sum)
subNEI <- NULL
plot3 <- ggplot(freqPerYear, aes(year, Emissions, fill=type))+ guides(fill=FALSE)+geom_bar(stat="identity")+facet_grid(.~type) +xlab("Year") + ylab("PM2.5 (tons)") + ggtitle("PM2.5 Total Emissions in Baltimore City, Marland (by Type)")
print(plot3)
## Save file and close device
#ggsave(plot3, file="plot3.png")
}
plot3()
## Warning: package 'ggplot2' was built under R version 3.2.4
plot4 <- function(){
combustion <-grepl("comb", SCC$SCC.Level.One, ignore.case=TRUE)
coal <- grepl("coal", SCC$SCC.Level.Four, ignore.case=TRUE)
coalCombustion <- (coal & combustion)
ccSCC <- SCC[coalCombustion,]$SCC
SCC <- NULL
coal <- NULL
combustion <- NULL
NEI$year <- as.factor(NEI$year)
ccNEI <- NEI[NEI$SCC %in% ccSCC,]
ccNEI <- ccNEI[,c("year", "Emissions")]
s <- aggregate(Emissions~year,ccNEI,FUN=sum)
ccNEI <- NULL
plot4 <- ggplot(s,aes(year,Emissions))+geom_bar(stat="identity")+xlab("Year")+ylab("PM2.5 (tons)")+ggtitle("Coal Combustion-related Emissions in US")
print(plot4)
## Save file and close device
#ggsave(plot4, file="plot4.png")
}
plot4()
plot5 <- function(){
vehicles <-grepl("vehicle", SCC$SCC.Level.Two, ignore.case = TRUE)
vSCC <- SCC[vehicles,]$SCC
NEI$year <- as.factor(NEI$year)
subNEI <- NEI[NEI$fips=="24510", c("SCC", "Emissions", "year")]
vNEI <- subNEI[subNEI$SCC %in% vSCC, ]
vNEI <- vNEI[,c("year", "Emissions")]
s <- aggregate(Emissions~year, vNEI, FUN=sum)
vNEI <- NULL
plot5 <- ggplot(s,aes(year,Emissions))+geom_bar(stat="identity")+xlab("Year")+ylab("PM2.5 (tons)")+ggtitle("Emissions from Vehicles in Baltimore City, Maryland")
print(plot5)
## Save file and close device
#ggsave(plot5, file="plot5.png")
}
plot5()
plot6 <- function(){
vehicles <-grepl("vehicle", SCC$SCC.Level.Two, ignore.case = TRUE)
vSCC <- SCC[vehicles,]$SCC
NEI$year <- as.factor(NEI$year)
subNEI <- NEI[NEI$fips=="24510" | NEI$fips == "06037", c("fips", "SCC", "Emissions", "year")]
subNEI$fips <- gsub("24510", "Baltimore City", subNEI$fips)
subNEI$fips <- gsub("06037", "LA County, California", subNEI$fips)
subNEI$fips <- as.factor(subNEI$fips)
vNEI <- subNEI[subNEI$SCC %in% vSCC, ]
subNEI <- NULL
vNEI <- vNEI[,c("fips", "year", "Emissions")]
s <- aggregate(Emissions~year+fips, vNEI,FUN=sum)
plot6 <- ggplot(s,aes(year, Emissions, fill=fips))+geom_bar(stat="identity")+facet_grid(.~fips)+xlab("Year")+ylab("PM2.5 (tons)")+ggtitle("PM2.5 Emissions from Motor Vehicles in LA County and Baltimore City")+guides(fill=FALSE)
print(plot6)
## Save file and close device
#ggsave(plot6, file="plot6.png")
## graph with another method
#png(file = "plot6_V2.png")
#q <- ggplot(RoadData2, aes(x = factor(year), y = Emissions, fill = city))
#q1 <- q + stat_summary(fun.y = sum, position = position_dodge(), geom = "bar")
#q2 <- q1 + labs(title = "Motor Vehicle Emissions from LA County, CA and Baltimore, MD", x = "Year", y = expression ( "Emissions: " * PM[2.5](tons), fill = "city"))
#q2 + scale_y_continuous(breaks=seq(0,4500,500))
#dev.off()
}
plot6()
For each plot you should