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 about the NEI can be obtained 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 is used 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:

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.

Source Classification Code Table (Source_Classification_Code.rds): This table provides a mapping from the SCC digit strings int he 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”.

Assignment

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. R package is used to support the analysis.

Questions

The following questions and tasks in exploratory analysis is addressed. For each question / task a single plot is made.

  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?

# Johns Hopkins University's "Exploratory Data Analysis".
# Date: 20.09.2014 - 21.09.2014
# Course Project 2
# File name: CompleteCode.R
# Project location: https://class.coursera.org/exdata-006/human_grading/view/courses/972591/assessments/4/submissions
# Data file location: https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip

library(plyr)
library(ggplot2)

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

# Read the data file
## This first line will likely take a few seconds. Be patient!
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")

totalPM25ByYear <- tapply(NEI$Emissions, NEI$year, sum)

plot(names(totalPM25ByYear), totalPM25ByYear, type = "l",
     xlab = "Year", ylab = expression("Total" ~ PM[2.5] ~ "Emissions (tons)"),
     main = expression("Total US" ~ PM[2.5] ~ "Emissions by Year"))

plot of chunk unnamed-chunk-1

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

BaltimoreCity <- subset(NEI, fips == "24510")

totalPM25ByYear <- tapply(BaltimoreCity$Emissions, BaltimoreCity$year, sum)

plot(names(totalPM25ByYear), totalPM25ByYear, type = "l", xlab = "Year", 
     ylab = expression("Total" ~ PM[2.5] ~ "Emissions (tons)"),
     main = expression("Total Baltimore City" ~ PM[2.5] ~ "Emissions by Year"))

plot of chunk unnamed-chunk-1

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

typePM25ByYear <- ddply(BaltimoreCity, .(year, type), function(x) sum(x$Emissions))
colnames(typePM25ByYear)[3] <- "Emissions"

qplot(year, Emissions, data = typePM25ByYear, color = type, geom = "line") +
      ggtitle(expression("Baltimore City" ~ PM[2.5] ~ 
      "Emissions by Source Type and Year")) + xlab("Year") +
      ylab(expression("Total" ~ PM[2.5] ~ "Emissions (tons)"))

plot of chunk unnamed-chunk-1

# Question 4:
# Across the United States, how have emissions from coal combustion-related
# sources changed from 1999-2008?

CoalCombustionSCC <- subset(SCC, EI.Sector %in% c("Fuel Comb - 
                            Comm/Institutional - Coal",
                            "Fuel Comb - Electric Generation - Coal",
                            "Fuel Comb - Industrial Boilers, ICEs - Coal"))
# Compare to Short.Name matching both Comb and Coal
CoalCombustionSCC1 <- subset(SCC, grepl("Comb", Short.Name) & grepl("Coal", 
                             Short.Name))

nrow(CoalCombustionSCC)
## [1] 68
nrow(CoalCombustionSCC1)
## [1] 91
d3 <- setdiff(CoalCombustionSCC$SCC, CoalCombustionSCC1$SCC)
d4 <- setdiff(CoalCombustionSCC1$SCC, CoalCombustionSCC$SCC)
length(d3)
## [1] 10
length(d4)
## [1] 33
CoalCombustionSCCCodes <- union(CoalCombustionSCC$SCC, CoalCombustionSCC1$SCC)
length(CoalCombustionSCCCodes)
## [1] 101
CoalCombustion <- subset(NEI, SCC %in% CoalCombustionSCCCodes)

coalCombustionPM25ByYear <- ddply(CoalCombustion, .(year, type), function(x) 
                                  sum(x$Emissions))
colnames(coalCombustionPM25ByYear)[3] <- "Emissions"

qplot(year, Emissions, data = coalCombustionPM25ByYear, color = type, 
      geom = "line") + ggtitle(expression("Coal Combustion" ~ PM[2.5] ~ 
      "Emissions by Source Type and Year")) + xlab("Year") + 
      ylab(expression  ("Total" ~ PM[2.5] ~ "Emissions (tons)"))

plot of chunk unnamed-chunk-1

# Question 5:
# How have emissions from motor vehicle sources changed from 1999-2008 in Baltimore City?

# Assume "Motor Vehicles" only means on road
BaltimoreCityMV <- subset(NEI, fips == "24510" & type=="ON-ROAD")

BaltimoreMVPM25ByYear <- ddply(BaltimoreCityMV, .(year), 
                               function(x) sum(x$Emissions))
colnames(BaltimoreMVPM25ByYear)[2] <- "Emissions"

qplot(year, Emissions, data=BaltimoreMVPM25ByYear, geom="line") +
      ggtitle(expression("Baltimore City" ~ PM[2.5] ~ "Motor Vehicle Emissions
      by Year")) + xlab("Year") + ylab(expression("Total" ~ PM[2.5] ~ 
      "Emissions (tons)"))
## Warning: font metrics unknown for character 0xa
## Warning: font metrics unknown for character 0xa
## Warning: font metrics unknown for character 0xa
## Warning: font metrics unknown for character 0xa
## Warning: font metrics unknown for character 0xa
## Warning: font metrics unknown for character 0xa

plot of chunk unnamed-chunk-1

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


# Assume "Motor Vehicles" only means on road
MV <- subset(NEI, (fips == "24510" | fips == "06037") & type=="ON-ROAD")
# Use more meaningful variable names
MV <- transform(MV, region = ifelse(fips == "24510", "Baltimore City", 
                "Los Angeles County"))

MVPM25ByYearAndRegion <- ddply(MV, .(year, region), function(x) 
                               sum(x$Emissions))
colnames(MVPM25ByYearAndRegion)[3] <- "Emissions"

# Create a plot normalized to 1999 levels to better show change over time
Balt1999Emissions <- subset(MVPM25ByYearAndRegion, year == 1999 & 
                            region == "Baltimore City")$Emissions
LAC1999Emissions <- subset(MVPM25ByYearAndRegion, year == 1999 & 
                           region == "Los Angeles County")$Emissions
MVPM25ByYearAndRegionNorm <- transform(MVPM25ByYearAndRegion,
                                       EmissionsNorm = ifelse(region == 
                                       "Baltimore City",
                                       Emissions / Balt1999Emissions,
                                       Emissions / LAC1999Emissions))

qplot(year, EmissionsNorm, data=MVPM25ByYearAndRegionNorm, geom="line", 
      color=region) + ggtitle(expression("Total" ~ PM[2.5] ~
      "Motor Vehicle Emissions Normalized to 1999 Levels")) + xlab("Year") +
      ylab(expression("Normalized" ~ PM[2.5] ~ "Emissions"))

plot of chunk unnamed-chunk-1