-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
41 lines (32 loc) · 1.5 KB
/
plot3.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
# 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")
# Sampling
NEI_sampling <- NEI[sample(nrow(NEI), size=5000, replace=F), ]
# Baltimore City, Maryland == fips
MD <- subset(NEI, fips == 24510)
MD$year <- factor(MD$year, levels=c('1999', '2002', '2005', '2008'))
# 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.
# Construct initial plot then add layers
p <- ggplot(data=MD, aes(x=year, y=log(Emissions)))
p <- p + facet_grid(. ~ type) + guides(fill=F)
p <- p + geom_boxplot(aes(fill=type)) + stat_boxplot(geom ='errorbar')
p <- p + labs(x = 'year', y = expression(paste('Log', ' of PM'[2.5], ' Emissions')))
p <- p + ggtitle('Emissions per Type in Baltimore City, Maryland')
p <- p + geom_jitter(alpha=0.10)
p <- p + theme(text = element_text(size = 08))
# Save output as .png
ggsave(filename = 'plot3.png', scale = 1)
dev.off()