-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot1.R
More file actions
25 lines (22 loc) · 912 Bytes
/
Copy pathplot1.R
File metadata and controls
25 lines (22 loc) · 912 Bytes
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
# Code to generate the first plot
totalPerYear <- function(year) {
# Compute the total amount of Pm2.5 per year
print(paste("Got year:", as.character(year)))
idxToUse <- (NEI$yearFactor == year)
subValue <- NEI$Emissions[idxToUse]
sum(subValue)
}
# Read the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")
NEI$yearFactor <- as.factor(NEI$year)
NEI$SourceFactor <- as.factor(NEI$SCC)
year.level <- levels(NEI$yearFactor)
tonsPerYear<- sapply(year.level, totalPerYear)
myModel <- lm(tonsPerYear ~ as.numeric(year.level))
png("plot1.png", width=480, height=480)
plot(names(tonsPerYear), tonsPerYear, xlab="Year", ylab="PM 2.5 (Tons)", main="")
abline(myModel, col="red")
legend(2006,6.8e6, c("raw data", "intercept"), lty=c(0,1), pch=c(1,-1), col = c("black", "red"))
title("Total PM2.5 emission in the United States from 1999 to 2008")
dev.off()