-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
409 lines (343 loc) · 14.7 KB
/
app.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Load libraries ####
##Create groups of libraries
datalibraries <- c("dplyr", "tidyverse", "janitor", "readxl", "readODS") #data reading and manipulation
vislibraries <- c("ggplot2", "ggthemes", "RColorBrewer", "scales") #pretty plots
applibraries <- c("shiny", "plotly", "gridlayout", "bslib", "DT") #libraries for app
##Install packages
#lapply(datalibraries, install.packages, character.only = TRUE)
#lapply(vislibraries, install.packages, character.only = TRUE)
#lapply(applibraries, install.packages, character.only = TRUE)
#devtools::install_github("rstudio/gridlayout") #install from github for this version of R
##Load libraries
lapply(datalibraries, library, character.only = TRUE)
lapply(vislibraries, library, character.only = TRUE)
lapply(applibraries, library, character.only = TRUE)
# Load data ####
##Map loop to download UK 2022 data from the UK Department for Energy Security and Net Zero
downloaded <- file.exists("UKGHG_2022.ods") #checks if file is downloaded in working directory
if(downloaded != T){ #if downloaded is not true
map2("https://assets.publishing.service.gov.uk/media/65c0d54663a23d000dc821ca/final-greenhouse-gas-emissions-2022-by-source-dataset.ods", #update this link when new data available
"UKGHG_2022.ods", download.file)} #else{print('data downloaded')} #name and download or print
##Read in ods file
GHG_UK22 <- read_ods(
path = "UKGHG_2022.ods",
sheet = 1, #define tab/sheet to read
col_names = TRUE, #use header row for column names
col_types = NULL, #guess data types
na = "", #treat blank cells as NA
skip = 0, #don't skip rows
formula_as_formula = FALSE, #values only
range = NULL,
row_names = FALSE, #no row names
strings_as_factors = TRUE) %>% #use factors
clean_names() %>% #clean column names to lowercase, with underscores
separate_wider_delim(source, delim = " - ", #separate source, define delimiter as " - "
names = c("source_1", "source_2", "source_3"), #name new columns
too_few = "align_start", too_many = "merge", #what to do with fewer or more than 3
cols_remove = FALSE) %>% #keep original column
separate_wider_delim(tes_category, delim = " - ", #separate category, define delimiter as " - "
names = c("category_1", "category_2"), #name new columns
too_few = "align_start", too_many = "merge", #what to do with fewer or more than 2
cols_remove = FALSE) %>% #keep original column
separate_wider_delim(activity, delim = " - ", #separate activity, define delimiter as " - "
names = c("activity_1", "activity_2"), #name new columns
too_few = "align_start", too_many = "merge", #what to do with fewer or more than 2
cols_remove = FALSE) %>% #keep original column
replace(is.na(.), "") #get rid of NA values, input blank
# Subset data ####
##Positive values only
GHG_UK22pos <- GHG_UK22 %>%
filter(emissions_mt_co2e >= "0") #subset filter only positive values
##Negative values only
GHG_UK22neg <- GHG_UK22 %>%
filter(emissions_mt_co2e <= "0") #subset filter only negative values
##Simplify
GHG_simp <- GHG_UK22 %>%
filter(year == 2022) %>%
group_by(tes_sector, tes_subsector, category_2, category_1, activity) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
replace(is.na(.), "") %>%
filter(Temissions >= "0") #subset filter only positive values
##Pivot - rows are greenhouse gas groups
GHG_wide <- GHG_UK22 %>%
pivot_wider(names_from = ghg_grouped, values_from=emissions_mt_co2e) %>%
replace(is.na(.), 0)
# Define filters ####
##Years, gases for user to select from
year <- unique(GHG_UK22$year)
GHG <- unique(GHG_UK22$ghg_grouped)
# Define colors ####
##Define custom color palette
colors <- c(
"#3CBD9C", #transport
"#F0A7CA", #buildings
"#F0C954", #industry
"#C24841", #electricity
"#96BC61", #ag green
"#5D7ED1", #lulucf blue
"#D38744", #fuel
"#A780BB" #waste purple
)
colors_3lvls <- c(
"#C24841", #CH4
"#3CBD9C", #CO2
"#F0C954" #N2O
)
# Build UI dashboard frame with grid layout ####
ui <- grid_page(
layout = c(
"header header header ",
"filter timeseries timeseries ",
"emissions emissions removals ",
"emissions emissions removals "
),
row_sizes = c(
"45px",
"1.8fr",
"0.2fr",
"1fr"
),
col_sizes = c(
"200px",
"0.8fr",
"1.2fr"
),
gap_size = "1rem",
grid_card(
area = "filter",
card_header("Filters"
),
card_body(
selectInput(
inputId = "Select_year",
label = "Year",
selected = max(year), #default select most recent
choices = year)
),
card_body(
selectInput(
inputId = "Select_GHG",
label = "GHG group",
selected = GHG, #default include all
choices = GHG)
),
),
grid_card_text(
area = "header",
content = "UK Greenhouse Gas Inventory",
alignment = "start",
is_title = FALSE
),
grid_card(
area = "timeseries",
full_screen = TRUE,
card_header("Time Series - Net Emissions by Sector"),
card_body(plotlyOutput(outputId = "t_plot"))
),
grid_card(
area = "removals",
full_screen = TRUE,
card_header("Negative Emissions (Removals)"),
card_body(plotlyOutput(outputId = "sb_neg_plot"))
),
grid_card(
area = "emissions",
full_screen = TRUE,
card_header("Greenhouse Gas Emissions"),
card_body(plotlyOutput(outputId = "sb_plot"))
)
)
# Server define inputs outputs ####
server <- function(input, output, session) {
produce_timeplot <- reactive({
## Time Series ####
#Plot time series
Sect_time <- GHG_UK22 %>%
filter(ghg_grouped %in% input$select_GHG) %>%
group_by(year, tes_sector) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
rename("Sector" = "tes_sector") %>% #rename variable
ggplot(aes(x=as.numeric(year), y=Temissions, color=Sector)) +
geom_line() +
xlab("") + ylab("Greenhouse Gas Emissions (Megatonnes CO2 eq.)") +
ggtitle("Greenhouse Gas Emissions of the UK per Sector Over Time") +
theme_few() +
scale_y_continuous(labels = comma) +
scale_x_continuous(breaks = pretty(c(1990:2020), n=6)) +
scale_color_brewer(palette = "Set2") +
theme(legend.position="bottom")
#Sect_time #view ggplot
#Make interactive with Plotly
t_int <- plotly::ggplotly(Sect_time) %>% #convert to interactive graph
layout(legend = list(
orientation = "h")) %>%
style(t_int, hovertemplate="Year: %{x:,.r} #define what hover label says, round x values
CO2eq: %{y:,.4r} kt") #round y values to 4 sig. figs
})
produce_eplot <- reactive({
## Emissions data format for sunburst ####
##Separate out variables summaries - all GHGs combined
sectors <- GHG_UK22pos %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_sector) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE))
subsectors <- GHG_UK22pos %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_sector, tes_subsector) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE))
cat_2 <- GHG_UK22pos %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
filter(category_2 != "") %>%
group_by(tes_sector, tes_subsector, category_2) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
unite('parents', c(tes_sector, tes_subsector), remove = TRUE, sep = " - ") #combine higher categories into parents
cat_1 <- GHG_UK22pos %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_sector, tes_subsector, category_2, category_1) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
mutate(parents = case_when(
category_2 != "" ~str_c(tes_sector,tes_subsector,category_2, sep = " - "),
category_2 == "" ~str_c(tes_sector,tes_subsector, sep = " - ")))
act_1 <- GHG_UK22pos %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_sector, tes_subsector, category_2, category_1, activity_1) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
mutate(parents = case_when(
category_2 != "" ~str_c(tes_sector,tes_subsector,category_2, category_1, sep = " - "),
category_2 == "" ~str_c(tes_sector,tes_subsector, category_1, sep = " - ")
))
act_2 <- GHG_UK22pos %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
filter(activity_2 != "") %>%
group_by(tes_sector, tes_subsector, category_2, category_1, activity_1, activity_2) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
mutate(parents = case_when(
category_2 != "" ~str_c(tes_sector,tes_subsector,category_2, category_1, activity_1, sep = " - "),
category_2 == "" ~str_c(tes_sector,tes_subsector, category_1, activity_1, sep = " - ")
))
##Now paste together distinct instances into dataframe with hierarchical structure - start simple, set root
GHGsb1 <- data.frame(
parents =c(rep("", 8)),
labels =c(paste0(sectors$tes_sector)),
values =as.numeric(c(paste0(sectors$Temissions)))
)
##Add id column
GHGsb1 <- GHGsb1 %>%
mutate(ids = labels)
##Now paste together distinct instances into dataframe with hierarchical structure - add on
GHGsb2 <- data.frame(
parents =c(paste0(subsectors$tes_sector), paste0(cat_2$parents), paste0(cat_1$parents),
paste0(act_1$parents), paste0(act_2$parents)),
labels =c(paste0(subsectors$tes_subsector), paste0(cat_2$category_2), paste0(cat_1$category_1),
paste0(act_1$activity_1), paste0(act_2$activity_2)),
values =as.numeric(c(paste0(subsectors$Temissions), paste0(cat_2$Temissions), paste0(cat_1$Temissions), paste0(act_1$Temissions), paste0(act_2$Temissions)))
)
##Add id column
GHGsb2 <- GHGsb2 %>%
unite('ids', c(parents, labels), remove = FALSE, sep = " - ") #combine columns for id
##Combine into one dataframe
GHGsb <- rbind(GHGsb1, GHGsb2)
## Sunburst Emissions with Plotly ####
sb <- plot_ly(GHGsb,
type = 'sunburst',
ids = ~ids,
labels = ~labels,
parents = ~parents,
values = ~values,
branchvalues = 'total', #parents are totals of children
insidetextorientation='radial',
maxdepth = 2, #how many levels to show
level = "",
sort = TRUE #sort by size
#width = 800, height = 800 #plot dimensions
) %>%
layout(
margin = list(l = 10, r = 10, b = 10, t = 10),
sunburstcolorway = colors,
extendsunburstcolors = TRUE
) %>%
style(sb, hovertemplate="%{label}
%{value:,.3r} Mt CO2 equivalents
%{percentParent:.1%} of %{parent} emissions
<extra></extra>")
})
produce_rplot <- reactive({
## Removals data format for sunburst ####
##Replace empty source 2 values with source 1
GHG_UK22neg$source_2[GHG_UK22neg$source_2 == ""] <- NA #also works to replace blanks or any value defined here with NA across a dataframe
GHG_UK22neg$source_2[is.na(GHG_UK22neg$source_2)] <- GHG_UK22neg$source_1[is.na(GHG_UK22neg$source_2)]
##Separate out variables summaries - all GHGs combined
subsectors_neg <- GHG_UK22neg %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_subsector) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE))
cat_neg <- GHG_UK22neg %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_subsector, tes_category) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE))
src_neg <- GHG_UK22neg %>%
filter(year == input$select_Year & ghg_grouped %in% input$select_GHG) %>%
group_by(tes_subsector, tes_category, source_2) %>%
summarize(Temissions = sum(emissions_mt_co2e, na.rm = TRUE)) %>%
unite('parents', c(tes_subsector, tes_category), remove = TRUE, sep = " - ") #combine higher categories into parents
##Now paste together distinct instances into dataframe with hierarchical structure - start simple, set root
GHGsb1_neg <- data.frame(
parents =c(rep("", 6)),
labels =c(paste0(subsectors_neg$tes_subsector)),
values =as.numeric(c(paste0(subsectors_neg$Temissions)))
)
##Add id column
GHGsb1_neg <- GHGsb1_neg %>%
mutate(ids = labels)
##Now paste together distinct instances into dataframe with hierarchical structure - add on
GHGsb2_neg <- data.frame(
parents =c(paste0(cat_neg$tes_subsector), paste0(src_neg$parents)),
labels =c(paste0(cat_neg$tes_category), paste0(src_neg$source_2)),
values =as.numeric(c(paste0(cat_neg$Temissions), paste0(src_neg$Temissions)))
)
##Add id column
GHGsb2_neg <- GHGsb2_neg %>%
unite('ids', c(parents, labels), remove = FALSE, sep = " - ") #combine columns for id
##Combine into one dataframe
GHGsb_neg <- rbind(GHGsb1_neg, GHGsb2_neg)
##Convert negative values to absolute (positive) values
GHGsb_neg$values <- abs(GHGsb_neg$values)
## Sunburst Removals with Plotly ####
sb_neg <- plot_ly(GHGsb_neg,
type = 'sunburst',
ids = ~ids,
labels = ~labels,
parents = ~parents,
values = ~values,
branchvalues = 'total', #parents are totals of children
insidetextorientation='radial',
maxdepth = 2, #how many levels to show
level = "",
sort = TRUE #sort by size
#width = 800, height = 800 #plot dimensions
) %>%
layout(
#margin = list(l = 10, r = 10, b = 10, t = 10),
sunburstcolorway = c(
"#3CBD9C", #forestry
"#F0C954", #grassland
"#5D7ED1", #peatland
"#C24841", #other
"#96BC61",
"#F0A7CA",
"#D38744",
"#A780BB"
),
extendsunburstcolors = TRUE
) %>%
style(sb_neg, hovertemplate="%{label}
%{value:,.3r} Mt CO2 equivalents
%{percentParent:.1%} of %{parent} removals
<extra></extra>")
})
output$t_plot <- renderPlotly({produce_timeplot})
output$sb_plot <- renderPlotly({produce_eplot})
output$sb_neg_plot <- renderPlotly({produce_rplot})
}
# Run app ####
shinyApp(ui, server)