-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot6.R
More file actions
41 lines (31 loc) · 1.64 KB
/
plot6.R
File metadata and controls
41 lines (31 loc) · 1.64 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
# Exploratory Data Analysis - Assignment 2 - Q.6
#setting Working Directory
setwd("C:/Users/Makarand/Desktop/Coursera/project")
# Load ggplot2 library
require(ggplot2)
# Loading provided datasets from local machine
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("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?
# Generate the graph in the same directory as the source code
png('plot6.png')
ggplot(data=DF, aes(x=year, y=Emissions)) + geom_bar(stat = "identity") + guides(fill=F) +
ggtitle('Total Emissions of Motor Vehicle Sources\nLos Angeles County, California vs. Baltimore City, Maryland') +
ylab(expression('PM'[2.5])) + xlab('Year') + theme(legend.position='none') + facet_grid(. ~ City) +
geom_text(aes(label=round(Emissions,0), size=1, hjust=0.5, vjust=-1))
dev.off()