-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot6.R.txt
113 lines (90 loc) · 4.08 KB
/
plot6.R.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
library(data.table)
library(ggplot2)
#LoadData : Load NEI and SCC data into global variables if not already present.
# These variables are then accessed by plot functions.
# NEI and SCC are loaded as data.table class.
# If data files do not exist locally then they are downloaded
# and unzipped.
LoadData <- function() {
if (!file.exists("summarySCC_PM25.rds") ||
!file.exists("Source_Classification_Code.rds")) {
if (!file.exists("FNEI_data.zip")) {
print("Downloading FNEI_data.zip ...")
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip",
"FNEI_data.zip", mode="wb")
unzip("FNEI_data.zip")
}
}
#Note: NEI and SCC are set in global environment
if (!exists("NEI")) {
print("Loading NEI data ...")
NEI <<- as.data.table(readRDS("summarySCC_PM25.rds"))
}
if (!exists("SCC")) {
print("Loading SCC data ...")
SCC <<- as.data.table(readRDS("Source_Classification_Code.rds"))
}
}
#plot6: Plots graphs showing PM 2.5 comparisions of
# emissions from motor vehicles between
# Baltimore City and LA in USA
plot6 <- function () {
LoadData() #NEI & SCC are now in global environment
# Links:
# http://www.epa.gov/air/emissions/basic.htm
# http://www.epa.gov/air/emissions/index.htm
# http://www.epa.gov/cgi-bin/broker?polchoice=PM&_debug=0&_service=data&_program=dataprog.national_1.sas
# Studying the links above it is understood that we need to grep
# "Mobile Sources" from SCC level one and ON-ROAD point types from NEI
scc.mobile <- grepl("mobile", SCC$SCC.Level.One, ignore.case=TRUE)
scc <- SCC[scc.mobile,]$SCC
#subset data of motor vehicles in Baltimore
nei <- NEI[(fips == "24510") & (SCC %in% scc) & (type=="ON-ROAD"),
sum(Emissions), by=year]
setnames(nei, 2, "balt")
#subset data of motor vehicles in LA
nei.la <- NEI[(fips == "06037") & (SCC %in% scc) & (type=="ON-ROAD"),
sum(Emissions), by=year]
setnames(nei.la, 2, "la")
nei[, la := nei.la$la]
#add relative (in %) and absolute differences
nei[, c("balt.per","la.per", "balt.abs", "la.abs") :=
list(balt/max(balt), la/max(la),
balt - head(balt, n=1), la - head(la, n=1))]
#plot 3 graphs showing actual values, absolute differences and relative differences
par(mfrow = c(1,3), oma = c(0, 0, 3, 0))
plot(nei$year, nei$la, ylim=c(0, 4700),
type="n", xaxt="n",
xlab="Year", ylab="Emissions (in tons)")
lines(nei$year, nei$balt, col="red")
lines(nei$year, nei$la, col="blue")
axis(1, at=nei$year, labels=nei$year)
legend("topleft", lty=c(1,1), col=c("red", "blue"),
legend=c("Baltimore", "LA"))
plot(nei$year, nei$balt.abs, ylim=c(-300, 700),
type="n", xaxt="n",
xlab="Year", ylab="Absolute differeces (in tons)")
lines(nei$year, nei$balt.abs, col="red")
lines(nei$year, nei$la.abs, col="blue")
axis(1, at=nei$year, labels=nei$year)
legend("topleft", lty=c(1,1), col=c("red", "blue"),
legend=c("Baltimore", "LA"))
plot(nei$year, nei$balt.per,
type="n", xaxt="n",
xlab="Year", ylab="Relative %age differeces")
lines(nei$year, nei$balt.per, col="red")
lines(nei$year, nei$la.per, col="blue")
axis(1, at=nei$year, labels=nei$year)
legend("bottomleft", lty=c(1,1), col=c("red", "blue"),
legend=c("Baltimore", "LA"))
title("top", outer=TRUE, cex.main=2.0,
main="Motor vehicle PM 2.5 emissions comparision of Baltimore and LA")
#save the data to png
dev.copy(png, file="plot6.png", width=1000, height=500)
dev.off()
#reset par to original values
par(mfrow = c(1,1), oma = c(0,0,0,0))
#return the data to the caller
nei
}
plot6()