-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlant_Emissions-Exploratory-Final.R
211 lines (174 loc) · 8.12 KB
/
Plant_Emissions-Exploratory-Final.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
library(readxl)
library(dplyr)
library(lubridate)
library(ggplot2)
## let i refer to Pollutant
## let j refer to Pollutant Source (Plant Unit/Train)
## Combine data in one list for looping
train <- c("Unit 1", "Unit 2", "Unit 3", "Unit 4")
data <- list()
for (j in 1:4) {
data[[j]] <- read_xlsx("Plant-Emissions.xlsx",
sheet = train[j], na = c("", "NT", "S/D", "T/A"))
data[[j]][,"Date"] <- as.Date(data[[j]]$Date)
}
names(data) <- train
## Add periods for plotting
pos <- as.Date("1/1/2017", format = "%m/%d/%Y")
for (j in 1:4) {
data[[j]] <- mutate(data[[j]], Source = as.factor(train[j]),
Year = as.factor(year(Date)),
Quarter = as.factor(paste0(Year, " ", quarter(Date), "Q"))
)
}
## Combine data in one data frame for plotting
full <- data.frame()
for (j in 1:4) {
full <- rbind(full, data[[j]])
}
## Create Plots
## Create "pol" data frame for looping
pol <- data.frame(
type = c("SOx", "NOx", "CO", "PM"),
Dlim = c(700, 1000, 500, 150),
stringsAsFactors = FALSE
)
th <- theme(plot.title = element_text(size = 30, face = "bold"),
plot.subtitle = element_text(size = 18),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14),
strip.text.x = element_text(face = "bold", size = 20))
labs <- labs(subtitle = "2Q 2015 - 3Q 2017", y = "Concentration (mg/Nm3)")
ticks <- scale_x_date(breaks = as.Date(c("1/1/2016", "1/1/2017"),
format = "%m/%d/%Y"), labels = c("2016", 2017))
## 1. Create Scatter Plots for Exploratory Analysis
for (i in 1:4){
png(paste("Scatter", pol$type[i], ".png"),
width = 640, height = 480)
print({
ggplot(full, aes_string(x = "Date", y = pol$type[i])) +
facet_grid(facets = . ~ Source) + th + labs + ticks +
geom_point(shape = 1, size = 4) +
geom_point(data = filter_(full,
paste(pol$type[i], ">", pol$Dlim[i])),
col = "red", alpha = 0.5, size = 4) +
geom_hline(yintercept = pol$Dlim[i], col = "red") +
geom_text(aes(x = pos, y = pol$Dlim[i],
label = paste(pol$Dlim[i], "mg/Nm3"),
vjust = -0.5), size = 5) +
labs(title = paste(pol$type[i], "Emissions"))
})
dev.off()
}
## Massive SOx and NOx outliers were submitted in the company's self-monitoring
## reports for 4th quarter 2015. Even though the magnitude indicate these are
## highly probable erroneous data, these data cannot be removed from the presentation
## as these were already submitted to regulatory bodies. Instead, the plots were
## bound using limits in the y-axes to avoid the outliers to squish the plots down.
pol <- mutate(pol, ylim = c(1500, 1000, 500, 150))
## 2. Create bound box plots per whole Time
for (i in 1:4){
png(paste("BoxT", pol$type[i], ".png"),
width = 640, height = 480)
print({
ggplot(full, aes_string(x = "Source", y = pol$type[i])) + th + labs +
coord_cartesian(ylim = c(0, pol$ylim[i])) +
stat_boxplot(geom ="errorbar", width = 0.1, size = 0.8) +
geom_boxplot(fill = "turquoise", outlier.size = 4,
outlier.shape = 1) +
geom_hline(yintercept = pol$Dlim[i], col = "red") +
geom_text(aes(x = 2.5, y = pol$Dlim[i],
label = paste(pol$Dlim[i], "mg/Nm3"),
vjust = -0.5), size = 5) +
labs(title = paste(pol$type[i], "Emissions"))
})
dev.off()
}
## For SOx almost all values above the 3rd quartile of all Units exceed limits.
## These plots do not show that the company had been fully compliant since the
## last quarters of 2017.
## 3. Create bound box plots per year
for (i in 1:4){
png(paste("BoxY", pol$type[i], ".png"),
width = 640, height = 480)
print({
ggplot(full, aes_string(x = "Year", y = pol$type[i])) +
facet_grid(facets = . ~ Source) + th + labs +
coord_cartesian(ylim = c(0, pol$ylim[i])) +
stat_boxplot(geom ="errorbar", width = 0.1, size = 0.8) +
geom_boxplot(fill = "turquoise", outlier.size = 4,
outlier.shape = 1) +
geom_hline(yintercept = pol$Dlim[i], col = "red") +
geom_text(aes(x = 2.5, y = pol$Dlim[i],
label = paste(pol$Dlim[i], "mg/Nm3"),
vjust = -0.5), size = 5) +
labs(title = paste(pol$type[i], "Emissions"))
})
dev.off()
}
## Even though box plots plotted per year already shows the great improvement of
## the company in following regulatory limits by year 2017, it did not show the
## progressive improvement within 2017 particularly in SOx for which 2017 box
## plots still have whiskers extending beyond the regulatory limits.
## 4. Create bound box plots per quarter
for (j in 1:4) {
for (i in 1:4){
png(paste("BoxQ", pol$type[i], train[j], ".png"),
width = 640, height = 480)
print({
ggplot(data[[j]], aes_string(x = "Quarter", y = pol$type[i])) +
coord_cartesian(ylim = c(0, pol$ylim[i])) + th + labs +
stat_boxplot(geom ="errorbar", width = 0.1, size = 0.8) +
geom_boxplot(fill = "turquoise", outlier.size = 4,
outlier.shape = 1) +
geom_hline(yintercept = pol$Dlim[i], col = "red") +
geom_text(aes(x = 2.5, y = pol$Dlim[i],
label = paste(pol$Dlim[i], "mg/Nm3"),
vjust = -0.5), size = 5) +
labs(title = paste(train[j], pol$type[i], "Emissions"))
})
dev.off()
}
}
## Plotting per quarter highlights the effect of extreme outlier data for
## 2015 4th Quarter SOx values of all Units.
## 5. Create bound box plots per year in 2015-2016; quarter in 2017
## It was noted that this form of summarization skews the x-axis scale and other
## plot types (ie, scatter plot) may be better for showing improvement over time
full <- mutate(full, Period = ifelse(Date < pos, levels(Year)[Year],
levels(Quarter)[Quarter]))
for (i in 1:4){
png(paste("BoxP", pol$type[i], ".png"),
width = 1120, height = 480)
print({
ggplot(full, aes_string(x = "Period", y = pol$type[i])) +
facet_grid(facets = . ~ Source) + th + labs +
coord_cartesian(ylim = c(0, pol$ylim[i])) +
stat_boxplot(geom ="errorbar", width = 0.1, size = 0.8) +
geom_boxplot(fill = "turquoise", outlier.size = 4,
outlier.shape = 1) +
geom_hline(yintercept = pol$Dlim[i], col = "red") +
geom_text(aes(x = 2.5, y = pol$Dlim[i],
label = paste(pol$Dlim[i], "mg/Nm3"),
vjust = -0.5), size = 5) +
labs(title = paste(pol$type[i], "Emissions"))
})
dev.off()
}
## When data are plotted yearly in 2015 & 201 and quarterly in 2017, almost all
## of the box plots now have whiskers below regulatory limits. The medians are
## also progressing downwards from 1Q to 3Q 2017.
## These are the only plots that management found acceptable, amidst caution that
## the x-axis scale was skewed by the mixed period groupings.
## EXTRA: Analyze Exceedances
exlist <- list()
for (i in 1:4) {
exlist[[i]] <- filter_(full, paste(pol$type[i], ">", pol$Dlim[i]))
}
names(exlist) <- c("exSOx", "exNOx", "exCO", "exPM")
exdata <- data.frame()
for (i in 1:4) {
exdata <- distinct(bind_rows(exdata, exlist[[i]]))
}
write.csv(exdata, file = "Plant-Emissions-exceedances.csv", row.names = FALSE)
## END