-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1TotalByYear.r
44 lines (34 loc) · 1.12 KB
/
plot1TotalByYear.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
##
plot1TotalByYear <- function(dat = readNEIdata(),
file = "plot1TotalByYear.png",
dir = ".",
years = NULL) {
# Load libraries
library(data.table)
# Summarize data by year
# Sets name for total PM2.5 tons
dat <- dat[, sum(Emissions), by = year]
setnames(dat, c("year", "Total_PM2.5"))
# Subset for 'years' if not NULL
if(!is.null(years)) dat <- dat[year %in% years, ]
# Define plot parameters
pch <- 3 # vertical cross
main <- "Total PM2.5 by Year"
xlab <- ""
ylab <- "Tons PM2.5"
col = "red"
lwd <- 2
# Open PNG plot device to directory/file
filename <- ifelse(dir == ".", file, paste(dir, file, sep = "/"))
png(filename = filename)
# Set global parameters
par(pch = pch)
# Create plot
plot(dat$year, dat$Total_PM2.5, main = main, xlab = xlab, ylab = ylab, col = col)
# Add regression line
abline(lm(Total_PM2.5 ~ year, dat), lwd = lwd)
# Close PNG plot device
dev.off()
# Return filename
filename
}