-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
27 lines (23 loc) · 1.21 KB
/
plot4.R
File metadata and controls
27 lines (23 loc) · 1.21 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
library(dplyr)
library(ggplot2)
## This first line will likely take a few seconds. Be patient!
NEI <- readRDS("summarySCC_PM25.rds") %>% mutate(type = tolower(gsub('-', '', type)))
SCC <- readRDS("Source_Classification_Code.rds") %>% rename(type = Data.Category)
SCC$type <- tolower(SCC$type)
## filter the data to combustion-related sources
fullDescription <- paste(SCC$SCC.Level.One, SCC$SCC.Level.Two, SCC$SCC.Level.Three, SCC$SCC.Level.Four)
coalComb <- SCC[grepl('Comb', fullDescription) & grepl('Coal', fullDescription),]
## join with SCC data and summarize the data by year
summaryByYear <- NEI %>% inner_join(coalComb) %>% group_by(year) %>% summarise(pm25=sum(Emissions))
## fit a linear model to the data and plot it
model <- lm(pm25~year, summaryByYear)
## plotting
png(filename = "plot4.png", width = 640, height = 480, bg="transparent")
g <- ggplot(summaryByYear, aes(year, pm25))
g + geom_point() +
ggtitle(expression(atop("Total PM2.5 emissions per year", atop(italic("from coal combustion-related sources"), "")))) +
ylab("PM2.5 (tons)") +
geom_smooth(method = "lm") +
theme_bw()
dev.off()
## Based on our model, it shows that generally, total emissions of PM2.5 is decreasing from coal combustion-related sources