-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4TotalByYearCoal.r
43 lines (35 loc) · 1.35 KB
/
plot4TotalByYearCoal.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
##
plot4TotalByYearCoal <- function(dat = readNEIdata(srcCols = c("SCC", "EI.Sector")),
file = "plot4TotalByYearCoal.png",
dir = ".",
years = NULL) {
# Load libraries
library(data.table)
library(ggplot2)
# Summarize data by year and EI sector
# Sets name for total PM2.5 tons
dat <- dat[, sum(Emissions), by = c("year", "EI.Sector")]
setnames(dat, c("year", "EI.Sector", "Total_PM2.5"))
# Subset for 'years' if not NULL
if(!is.null(years)) dat <- dat[year %in% years, ]
# Subset for coal
isCoal <- function(txt) {grepl("coal", txt, ignore.case = TRUE)}
dat <- dat[sapply(dat$EI.Sector, isCoal), ]
# Define plot parameters
main <- "Total PM2.5 by Year from Coal"
geom <- c("point", "smooth")
method <- "lm"
xlab <- ""
ylab <- "Tons PM2.5"
dpi <- 90
width <- 1440 / dpi
height <- 900 / dpi
# Create plot
p <- qplot(year, Total_PM2.5, data = dat, geom = geom, method = method,
col = EI.Sector, main = main, xlab = xlab, ylab = ylab)
# Save plot
filename <- ifelse(dir == ".", file, paste(dir, file, sep = "/"))
ggsave(filename = filename, plot = p, width = width, height = height, dpi = dpi)
# Return filename
filename
}