-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
57 lines (42 loc) · 1.55 KB
/
plot2.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
51
52
53
54
55
56
57
# Checking if the files exist in the current directory. If not, they will be
# downloaded and working directory will be set to a new one.
if(!file.exists("Source_Classification_Code.rds") &
!file.exists("summarySCC_PM25.rds")) {
tmp <- tempfile()
download.file(
"https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip",
tmp)
dir.create("./pm2.5_project_data")
old.dir <- getwd()
setwd("pm2.5_project_data")
}
# Checking for the existance of 'zip' package. If not, it will be installed
if (!require("zip", quietly = TRUE)) {
install.packages("zip")
library(zip)
} else {
library(zip)
}
# unzipping the downloaded files
unzip(tmp, exdir = ".")
# loading data
SCC <- readRDS("Source_Classification_Code.rds")
NEI <- readRDS("summarySCC_PM25.rds")
# Processing
nei_baltimore <- subset(NEI, fips == "24510")
yeardata <- with(nei_baltimore, tapply(Emissions, year, sum))
yeardata1 <- data.frame(Year = names(yeardata), Total_Emissions = yeardata)
# Plotting data
png("plot2.png", height=461, width=762, units="px")
with(yeardata1, plot(Year, Total_Emissions, pch = 19,
main = "Total Emissions in Maryland, Baltimore from 1999 to 2008",
xlim = c(1998, 2008),
xlab="Year", ylab="Total Emissions (tons)"
))
lmm <- lm(Total_Emissions ~ as.integer(Year), yeardata1)
abline(lmm, lwd = 2)
title()
dev.off()
# Closing
setwd(old.dir)
#=================================== END =======================================