Introduction

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). More information information about the NEI available 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 for this assignment are for 1999, 2002, 2005, and 2008.

Data

The data for this assignment are available from the course web site as a single zip file: Data for Peer Assessment[29Mb]

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.

  • \(\color{blue}{\text{fips}}\): A five-digit number (represented as a string) indicating the U.S. county
  • \(\color{blue}{\text{SCC}}\): The name of the source as indicated by a digit string
  • \(\color{blue}{\text{Pollutant}}\): A string indicating the pollutant
  • \(\color{blue}{\text{Emissions}}\): Amount of PM2.5 emitted, in tons
  • \(\color{blue}{\text{type}}\): The type of source (point, non-point, on-road, or non-road)
  • \(\color{blue}{\text{year}}\): The year of emissions recorded

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”.

Loading and preprocessing the data

Download file and unzip the archive to the current working directory

fileurl<-"https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip"
download.file(fileurl,destfile=paste0(getwd(),"/NEI_data.zip"),method = "curl")
unzip("NEI_data.zip",exdir="./")

Reading the data in

NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")

Assignment

The overall goal 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. Analysis must address the following questions and tasks. For each question/task there should be made a single plot. Unless specified, any plotting system in R can be used to create a plot.

Question 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.

total_year<-tapply(NEI$Emissions, NEI$year, sum, na.rm=TRUE)
##png("plot1.png", width=640, height=640)
par(mfrow=c(1,1),mar=c(5,5,4,2))
barplot(total_year/1000, names.arg = names(total_year), col="dodgerblue2", main="Emissions of PM2.5 by year", 
        xlab = "Year", ylab="Amount of emissions (kilotons)", ylim = range(0,total_year/1000) * 1.1)
lines(names(total_year), total_year/1000, lwd=2, col="blue") ##dev.off()

Question 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.

NEI_Baltimore<-subset(NEI, fips == "24510")
Baltimore_year<-tapply(NEI_Baltimore$Emissions, NEI_Baltimore$year, sum, na.rm=TRUE)

##png("plot2.png", width=640, height=640)
par(mfrow=c(1,1),mar=c(5,5,4,2))
barplot(Baltimore_year, names.arg = names(Baltimore_year), col="dodgerblue3", 
        main="Emissions of PM2.5 by year in Baltimore City", xlab = "Year", 
        ylab="Amount of emissions (tons)", ylim = range(0,Baltimore_year)*1.1) ##dev.off()

Question 3

Of the four types of sources indicated by the typetype (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.

library(ggplot2)
library(reshape2)

Bmelt<-melt(NEI_Baltimore,id=c("year","type"), measure.vars = "Emissions")
Bcast<-dcast(Bmelt,year~type,sum)
Baltimore_type<-melt(Bcast,id=c("year"),measure.vars=names(Bcast)[-c(1)])

##png("plot3.png", width=640, height=480)
gt<-ggplot(Baltimore_type, aes(x=year,y=value)) + geom_col(width = 1,mapping=aes(fill=variable)) + 
    facet_grid(.~variable) + theme_bw() + geom_smooth(method = "lm") +
    labs(title="Emissions of PM2.5 in Baltimore City by type") +
    xlab("Year") + ylab("Amount of PM2.5 emissions (tons)")+
    theme(text= element_text(size = 10), plot.title = element_text(size=13, hjust = 0.5, face="bold"), 
          plot.margin = margin(1, 1, 1, 1, "cm"))
gt ##dev.off()

Question 4

Question 5

How have emissions from motor vehicle sources changed from 1999–2008 in Baltimore City?

gl<-grepl("(.*)(Highway Veh)(.*)",SCC$Short.Name)
SCC_MotorVeh<-SCC[gl,]
NEI_Baltimore_MotorVeh<-subset(NEI_Baltimore, SCC %in% SCC_MotorVeh$SCC)
NEI_Baltimore_MotorVeh_year<-tapply(NEI_Baltimore_MotorVeh$Emissions, 
                                    NEI_Baltimore_MotorVeh$year, sum, na.rm=TRUE)

##png("plot5.png", width=640, height=480)
par(mfrow=c(1,1),mar=c(5,5,4,2))
barplot(NEI_Baltimore_MotorVeh_year, names.arg = names(NEI_Baltimore_MotorVeh_year), col="darkorchid2", 
     main="Emissions of PM2.5 from motor vehicles by year in Baltimore City", 
     xlab = "Year", ylab="Amount of emissions (tons)", 
     ylim = range(0,NEI_Baltimore_MotorVeh_year)*1.1) ##dev.off()

Question 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?

NEI_LA <-subset(NEI, fips=="06037")
NEI_LA_MotorVeh<-subset(NEI_LA,SCC %in% SCC_MotorVeh$SCC)
NEI_LA_MotorVeh_year<-tapply(NEI_LA_MotorVeh$Emissions,
                             NEI_LA_MotorVeh$year, sum, na.rm=TRUE)
Balt_LA<-data.frame(year=as.numeric(names(NEI_LA_MotorVeh_year)),
                    Baltimore.Emissions = NEI_Baltimore_MotorVeh_year,
                    LA.Emissions = NEI_LA_MotorVeh_year)

rng<-c(0,max(Balt_LA$Baltimore.Emissions,Balt_LA$LA.Emissions)+1000)
##png("plot6.png", width=480, height=720)
par(mfrow=c(1,1),mar=c(5,5,4,2))
plot(Balt_LA$year, Balt_LA$LA.Emissions, type="l", lwd=2, pch=19,  
     col="darkorchid3", main="Emissions of PM2.5 from motor vehicles by year", 
     xlab = "Year", ylab="Amount of emissions (tons)", ylim=rng)
lines(Balt_LA$year,Balt_LA$Baltimore.Emissions, lwd=2, col="darkorchid3")
points(Balt_LA$year,Balt_LA$Baltimore.Emissions, lwd=2, pch=19, col="darkorchid3")
points(Balt_LA$year,Balt_LA$LA.Emissions, lwd=2, pch=19, col="chartreuse3")
lines(Balt_LA$year,Balt_LA$LA.Emissions, lwd=2, col="chartreuse3")
legend("topright", lwd=2, col=c("darkorchid3", "chartreuse3"), 
       legend=c("Baltimore City","Los Angeles County"), bty="n") ##dev.off()