-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot6.txt
More file actions
99 lines (80 loc) · 3.27 KB
/
Copy pathplot6.txt
File metadata and controls
99 lines (80 loc) · 3.27 KB
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
## Instructions for plot6.R
## ------------------------------------------------------------------------------------
## 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?
## Download and read in data
download.file(url="https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip",
destfile="Exploratory2.zip",
method="curl")
unzip(zipfile="Exploratory2.zip")
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")
### --------------------------------------------------------------------------###
### Prep work
### --------------------------------------------------------------------------###
## Load ggplot2 and data.table
library(ggplot2)
library(data.table)
## Subset for vehicle emissions and baltimore
## These were found visually by looking at grep("Vehicle|vehicle",names(table(SCC$EI.Sector)), value=TRUE)
## Find the SCC codes which refer to vehicle emissions, and make a character vector since they are characters in NEI
vehicleSCC <- as.character(SCC[grep("Vehicle", SCC$EI.Sector),]$SCC)
## Subset NEI according to the SCC values for vehicle emissions combustion and for Baltimore
baltvehicle <- NEI[NEI$SCC %in% vehicleSCC & NEI$fips=="24510",]
baltvehicle <- data.table(baltvehicle)
## Repeat the previous step but for Los Angeles
lavehicle <- NEI[NEI$SCC %in% vehicleSCC & NEI$fips=="06037",]
lavehicle <- data.table(lavehicle)
## Sum Emissions by year
plot6balt <- baltvehicle[,sum(Emissions), by=year]
plot6la <- lavehicle[,sum(Emissions), by=year]
### --------------------------------------------------------------------------###
### Now we make the SIXTH plot and save it
### --------------------------------------------------------------------------###
png("/Users/Mario/Desktop/ExploratoryDataAssignment2/plot6.png", width=600, height=600)
## Set up the first plot, Total Emissions ~ Year
par(mar=c(5, 8, 4, 4) + 0.1)
plot(V1 ~ year,
data=plot6balt,
xlab="Year",
ylab="",
type="b",
col="red",
lwd=2,
pch=19,
axes=F,
ylim=c(60,380),
main="Total Vehicle Emissions by Year, Baltimore vs. Los Angeles"
)
## Label the Baltimore y-axis and points
mtext(2,text="Baltimore Vehicle Emissions in Tons", line=2.2)
text(plot6balt[,year], plot6balt[,V1], labels=round(plot6balt[,V1],0), pos=c(3,1,3,1))
axis(2, ylim=c(60,380),col="black",lwd=2)
## Add a completely new plot ontop of the old one for Los Angeles
par(new=T)
plot(V1 ~ year,
data=plot6la,
xlab="",
ylab="",
type="b",
col="blue",
lwd=2,
pch=19,
axes=F,
ylim=c(3800,4800),
main=""
)
## Label the Los Angeles y-axis and points
mtext(2,text="Los Angeles Vehicle Emissions in Tons", line=5.7)
text(plot6la[,year], plot6la[,V1], labels=round(plot6la[,V1],0), pos=c(1,1,3,1))
axis(2, ylim=c(3800,4800), col="black", line=3.7)
## Label the x axis
axis(side=1, at=c(1999,2002,2005,2008))
## Add a legend to the top right
legend("topright",
legend=c("Baltimore Vehicle Emissions in Tons", "Los Angeles Vehicle Emissions in Tons"),
col=c("red","blue"),
lty=1,
lwd=2)
dev.off()