“Analysing PM2.5 pollution data”

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.

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
setwd("C:/My Work/Data Analytics/Coursera/Coursera/Exploratory Data Analysis")
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")

head(NEI)
##     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
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
## 1 External Combustion Boilers Electric Generation
## 2 External Combustion Boilers Electric Generation
## 3 External Combustion Boilers Electric Generation
## 4 External Combustion Boilers Electric Generation
## 5 External Combustion Boilers Electric Generation
## 6 External Combustion Boilers Electric Generation
##                 SCC.Level.Three
## 1               Anthracite Coal
## 2               Anthracite Coal
## 3 Bituminous/Subbituminous Coal
## 4 Bituminous/Subbituminous Coal
## 5 Bituminous/Subbituminous Coal
## 6 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

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

year_pm<-aggregate(Emissions ~ year, data = NEI, sum)
plot(year_pm,type='l')

g=ggplot(year_pm,aes(year,Emissions))+geom_line()
g

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.

baltimore<-filter(NEI,fips == "24510")
baltimore_year_pm<-aggregate(Emissions ~ year, data = baltimore, sum)
g=ggplot(baltimore_year_pm,aes(year,Emissions))+geom_line()
g

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.

distinct(baltimore,type)
##       type
## 1    POINT
## 2 NONPOINT
## 3  ON-ROAD
## 4 NON-ROAD
p <- ggplot(baltimore, aes(type, Emissions))
p + geom_boxplot()+ geom_jitter(width = 0.1)+ coord_flip()

baltimore_agg<-aggregate(Emissions~year+type,data=baltimore,FUN="sum")

p<-ggplot(baltimore_agg)  #,aes(x=year,y=Emissions,group=type))
p+geom_line(aes(x=year,y=Emissions,color=type),size=1.5)

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

coalRelated <- grepl("coal", SCC$Short.Name, ignore.case=TRUE) 

combustionSCC <- SCC[coalRelated,]$SCC

combustionNEI <- NEI[NEI$SCC %in% combustionSCC,]

head(combustionNEI)
##       fips        SCC Pollutant Emissions     type year
## 149  09001 2104001000  PM25-PRI     1.134 NONPOINT 1999
## 2277 09003 2104001000  PM25-PRI     3.842 NONPOINT 1999
## 4204 09005 2104001000  PM25-PRI     1.447 NONPOINT 1999
## 5967 09007 2104001000  PM25-PRI     1.574 NONPOINT 1999
## 7998 09009 2104001000  PM25-PRI     2.183 NONPOINT 1999
## 9979 09011   10100217  PM25-PRI   479.907    POINT 1999
p<-ggplot(combustionNEI,aes(x=as.factor(year),y=Emissions))
p+geom_bar(stat="identity",fill="grey",width=0.75)+theme_bw()

  labs(x="Year", y=expression("Total PM"[2.5]*" Emission (10^5 Tons)"),
       title="Coal Combustion-related Sources")
## $x
## [1] "Year"
## 
## $y
## expression("Total PM"[2.5] * " Emission (10^5 Tons)")
## 
## $title
## [1] "Coal Combustion-related Sources"
## 
## attr(,"class")
## [1] "labels"

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

vehicles <- grepl("vehicle", SCC$SCC.Level.Two, ignore.case=TRUE)

vehiclesSCC <- SCC[vehicles,]$SCC
vehiclesNEI <- NEI[NEI$SCC %in% vehiclesSCC,]
vehiclesNEI<-vehiclesNEI[vehiclesNEI$fips== "24510",]
#vahiclesNEIag<-aggregate(Emission~year,data=vehiclesNEI,FUN=sum)

p<-ggplot(vehiclesNEI,aes(x=as.factor(year),y=Emissions))
p+geom_bar(stat="identity",fill="grey",width=0.75)+
  labs(x="Year", y=expression("Total PM"[2.5]*" Emission (10^5 Tons)"),
       title="Motor Vehicle -related Sources")

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?

vehicleBaltimore<-vehiclesNEI

vehicles <- grepl("vehicle", SCC$SCC.Level.Two, ignore.case=TRUE)
vehiclesSCC <- SCC[vehicles,]$SCC
vehiclesNEI <- NEI[NEI$SCC %in% vehiclesSCC,]
vehiclesLA<-vehiclesNEI[vehiclesNEI$fips== "06037",]
vehiclesLA$city<-'Los Angeles County'

vehicleBaltimore$city<-'Baltimore City'

vehiclesALL<-rbind(vehiclesLA,vehicleBaltimore)


vehiclesALag<-aggregate(Emissions~year+city,data=vehiclesALL,FUN="sum")

p<-ggplot(vehiclesALag,aes(x=year,y=Emissions,fill=city))
p+geom_bar(aes(fill=year),stat="identity")+
  facet_grid(scales="free", space="free", .~city) +
  guides(fill=FALSE) + theme_bw() +
  labs(x="Year", y="Total PM Emission",
                            title="Motor Vehicle -related Sources")