-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot6.R
50 lines (39 loc) · 1.9 KB
/
plot6.R
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
# load required packages
packages <- c("RMySQL", "ggplot2")
sapply(packages, require, character.only = TRUE, quietly = TRUE)
# Set working directory
setwd("~/Dropbox/Coursera/ExData_Plotting2/")
# Check if data directory exists and if not, create one
if(!file.exists("data")) {
dir.create("data")
}
# Loading provided datasets - loading from local machine
NEI <- readRDS("./data/summarySCC_PM25.rds")
SCC <- readRDS("./data/Source_Classification_Code.rds")
NEI$year <- factor(NEI$year, levels=c('1999', '2002', '2005', '2008'))
# Baltimore City, Maryland
# Los Angeles County, California
MD.onroad <- subset(NEI, fips == '24510' & type == 'ON-ROAD')
CA.onroad <- subset(NEI, fips == '06037' & type == 'ON-ROAD')
# Aggregate
MD.DF <- aggregate(MD.onroad[, 'Emissions'], by=list(MD.onroad$year), sum)
colnames(MD.DF) <- c('year', 'Emissions')
MD.DF$City <- paste(rep('MD', 4))
CA.DF <- aggregate(CA.onroad[, 'Emissions'], by=list(CA.onroad$year), sum)
colnames(CA.DF) <- c('year', 'Emissions')
CA.DF$City <- paste(rep('CA', 4))
DF <- as.data.frame(rbind(MD.DF, CA.DF))
# 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?
# Construct initial plot then add layers
plot <- ggplot(data=DF, aes(x=year, y=Emissions))
plot <- plot + geom_bar(aes(fill=year), stat="identity") + guides(fill=F)
plot <- plot + ggtitle('Total Emissions of Motor Vehicle Sources\nLos Angeles County, California vs. Baltimore City, Maryland')
plot <- plot + ylab(expression('PM'[2.5])) + xlab('Year')
plot <- plot + theme(legend.position='none')
plot <- plot + facet_grid(. ~ City)
plot <- plot + geom_text(aes(label=round(Emissions,0), size=1, hjust=0.5, vjust=-1))
# Save output as .png and turn off graphics device
ggsave(filename = 'plot6.png', scale = 1)
dev.off()