Shiny app graphing NZ CO2 (eqv) emissions from 1990 to 2010 https://shiny.petras.space/Emissions/
rstats
statistics
rshiny
climate-change
new-zealand

app.R 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. library(shiny)
  2. library(ggplot2)
  3. library(scales)
  4. library(tidyverse)
  5. library(readxl)
  6. library(shinycssloaders)
  7. library(RColorBrewer)
  8. # Original data from
  9. # https://catalogue.data.govt.nz/dataset/
  10. # new-zealands-energy-outlook/resource/3caad31e-aa96-4c94-8f54-1000518a0690?inner_span=True
  11. # Data loading ----
  12. Emissions.Sector <- read_excel("Emissions1990-2010.xlsx",
  13. sheet = "Sector")
  14. Emissions.Fuel <- read_excel("Emissions1990-2010.xlsx",
  15. sheet = "Fuel")
  16. sectors.a <- unique(Emissions.Sector$Sector)
  17. sectors <- unique(Emissions.Sector$Sector)[-1]
  18. sector.col <- data.frame(Sector=sectors.a,
  19. col=brewer.pal(length(sectors.a), "Set3"))
  20. Emissions.Sector <- left_join(Emissions.Sector, sector.col) %>%
  21. mutate(col = as.character(col))
  22. Sector.Tot <- filter(Emissions.Sector, Source == "Total")
  23. Emissions.Fuel <- cbind(Emissions.Fuel,
  24. col=brewer.pal(nrow(Emissions.Fuel), "Set2")) %>%
  25. mutate(col=as.character(col))
  26. fuels <- Emissions.Fuel$Fuel
  27. # gather(comf, key="Year", value="Co2eqv", as.character(1990:2010))
  28. Sector.Fuels <- function(sector) {
  29. sf <- filter(Emissions.Sector, Sector == sector)
  30. if (nrow(sf) > 1) {
  31. sf <- filter(sf, !grepl("Total", Source))
  32. }
  33. return(sf)
  34. }
  35. # UI ----
  36. ui <- navbarPage("NZ Emissions", collapsible = TRUE,
  37. # Sector tab ----
  38. tabPanel("Between sectors",
  39. h3("Emissions between sectors"),
  40. plotOutput("secPlot") %>% withSpinner(type=5),
  41. fluidRow(
  42. column(5, offset=1,
  43. h4("Options"),
  44. sliderInput("secyears", "Date range",
  45. min=1990, max=2010, value=c(1990, 2010),
  46. sep="", step=1, width="100%"),
  47. selectInput("secscale", "Scale",
  48. choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
  49. checkboxInput("seclegend", "Show legend", value=TRUE),
  50. checkboxInput("secminc", "Set minimum", value=FALSE),
  51. conditionalPanel("input.secminc",
  52. numericInput("secmin", label=NULL, value=NA)),
  53. checkboxInput("secmaxc", "Set maximum", value=FALSE),
  54. conditionalPanel("input.secmaxc",
  55. numericInput("secmax", label=NULL, value=NA))
  56. ),
  57. column(5, offset=1,
  58. h4("Sectors"),
  59. checkboxGroupInput("secSectors", NULL,
  60. choices=sectors.a, selected = sectors.a)
  61. )
  62. )
  63. ),
  64. # Within tab ----
  65. tabPanel("Within sectors",
  66. h3("Emissions within sectors"),
  67. plotOutput("wiPlot") %>% withSpinner(type=5),
  68. fluidRow(
  69. column(5, offset=1,
  70. h4("Options"),
  71. sliderInput("wiyears", "Date range",
  72. min=1990, max=2010, value=c(1990, 2010),
  73. sep="", step=1, width="100%"),
  74. selectInput("wiscale", "Scale",
  75. choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
  76. checkboxInput("wilegend", "Show legend", value=TRUE),
  77. checkboxInput("wiminc", "Set minimum", value=FALSE),
  78. conditionalPanel("input.wiminc",
  79. numericInput("wimin", label=NULL, value=NA)),
  80. checkboxInput("wimaxc", "Set maximum", value=FALSE),
  81. conditionalPanel("input.wimaxc",
  82. numericInput("wimax", label=NULL, value=NA))
  83. ),
  84. column(5, offset=1,
  85. h4("Sector"),
  86. selectInput("picksec", label=NULL, selected=sectors.a[3], choices=sectors.a)
  87. )
  88. )),
  89. # Fuel tab ----
  90. tabPanel("Between fuels",
  91. h3("Emissions between fuels"),
  92. plotOutput("fuPlot") %>% withSpinner(type=5),
  93. fluidRow(
  94. column(5, offset=1,
  95. h4("Options"),
  96. sliderInput("fuyears", "Date range",
  97. min=1990, max=2010, value=c(1990, 2010),
  98. sep="", step=1, width="100%"),
  99. selectInput("fuscale", "Scale",
  100. choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
  101. checkboxInput("fulegend", "Show legend", value=TRUE),
  102. checkboxInput("fuminc", "Set minimum", value=FALSE),
  103. conditionalPanel("input.fuminc",
  104. numericInput("fumin", label=NULL, value=NA)),
  105. checkboxInput("fumaxc", "Set maximum", value=FALSE),
  106. conditionalPanel("input.fumaxc",
  107. numericInput("fumax", label=NULL, value=NA))
  108. ),
  109. column(5, offset=1,
  110. h4("Fuels"),
  111. checkboxGroupInput("fuFuels", NULL,
  112. choices=fuels, selected = fuels)
  113. )
  114. )
  115. )
  116. )
  117. server <- function(input, output) {
  118. # Sector logic ----
  119. SecOverall <- reactive({
  120. st <- gather(Sector.Tot, key="Year",
  121. value="Co2eqv", as.character(1990:2010)) %>%
  122. mutate(Year = as.integer(Year),
  123. Sector = factor(Sector, levels=unique(Sector))) %>%
  124. # Logic for filtering out years, sectors here
  125. filter(Year %in% input$secyears[1]:input$secyears[2],
  126. Sector %in% input$secSectors) %>%
  127. mutate(Year = as.Date(paste0(Year, "-01-01")))
  128. return(st)
  129. })
  130. # Sector plot ----
  131. output$secPlot <- renderPlot({
  132. sov <- SecOverall()
  133. if (input$secyears[1] != input$secyears[2]) {
  134. ggplot(sov, aes(x=Year,
  135. y=Co2eqv, color=Sector)) -> s.p
  136. s.p + geom_line(size=1.5, na.rm=TRUE) -> s.p
  137. s.p + theme_linedraw() -> s.p
  138. s.p + scale_color_manual(values=unique(sov$col)) -> s.p
  139. } else {
  140. ggplot(sov, aes(x=Sector,
  141. y=Co2eqv, fill=Sector)) -> s.p
  142. s.p + geom_col(na.rm=TRUE) -> s.p
  143. s.p + theme_linedraw() -> s.p
  144. s.p + theme(axis.text.x = element_text(
  145. angle = -30, hjust = 0, vjust = 0)) -> s.p
  146. s.p + scale_fill_manual(values=unique(sov$col)) -> s.p
  147. }
  148. s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
  149. s.p + theme(legend.position = "right",
  150. legend.title = element_blank()) -> s.p
  151. s.p + ylab("CO₂ equivalent (kt)") -> s.p
  152. if (!input$seclegend) {
  153. s.p + theme(legend.position="none") -> s.p
  154. }
  155. mmx <- c(ifelse(input$secminc, input$secmin, NA),
  156. ifelse(input$secmaxc, input$secmax, NA))
  157. if (input$secscale == "sqrt(y)") {
  158. s.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> s.p
  159. } else if (input$secscale == "log10(y)") {
  160. s.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> s.p
  161. } else {
  162. s.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> s.p
  163. }
  164. return(s.p)
  165. })
  166. # Fuel logic ----
  167. FuOverall <- reactive({
  168. st <- gather(Emissions.Fuel, key="Year",
  169. value="Co2eqv", as.character(1990:2010)) %>%
  170. mutate(Year = as.integer(Year),
  171. Fuel = factor(Fuel, levels=unique(Fuel))) %>%
  172. # Logic for filtering out years, fuels here
  173. filter(Year %in% input$fuyears[1]:input$fuyears[2],
  174. Fuel %in% input$fuFuels) %>%
  175. mutate(Year = as.Date(paste0(Year, "-01-01")))
  176. return(st)
  177. })
  178. # Fuel plot ----
  179. output$fuPlot <- renderPlot({
  180. fov <- FuOverall()
  181. if (input$fuyears[1] != input$fuyears[2]) {
  182. ggplot(fov, aes(x=Year,
  183. y=Co2eqv, color=Fuel)) -> f.p
  184. f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
  185. f.p + theme_linedraw() -> f.p
  186. f.p + scale_color_manual(values=unique(fov$col)) -> f.p
  187. } else {
  188. ggplot(fov, aes(x=Fuel,
  189. y=Co2eqv, fill=Fuel)) -> f.p
  190. f.p + geom_col(na.rm=TRUE) -> f.p
  191. f.p + theme_linedraw() -> f.p
  192. f.p + theme(axis.text.x = element_text(
  193. angle = -30, hjust = 0, vjust = 0)) -> f.p
  194. f.p + scale_fill_manual(values=unique(fov$col)) -> f.p
  195. }
  196. f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
  197. f.p + theme(legend.position = "right",
  198. legend.title = element_blank()) -> f.p
  199. f.p + ylab("CO₂ equivalent (kt)") -> f.p
  200. if (!input$fulegend) {
  201. f.p + theme(legend.position="none") -> f.p
  202. }
  203. mmx <- c(ifelse(input$fuminc, input$fumin, NA),
  204. ifelse(input$fumaxc, input$fumax, NA))
  205. if (input$fuscale == "sqrt(y)") {
  206. f.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
  207. } else if (input$fuscale == "log10(y)") {
  208. f.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
  209. } else {
  210. f.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
  211. }
  212. return(f.p)
  213. })
  214. # Within logic ----
  215. WiSec <- reactive({
  216. st <- gather(Sector.Fuels(input$picksec), key="Year",
  217. value="Co2eqv", as.character(1990:2010)) %>%
  218. mutate(Year = as.integer(Year),
  219. Source = factor(Source, levels=unique(Source))) %>%
  220. # Logic for filtering out years, fuels here
  221. filter(Year %in% input$wiyears[1]:input$wiyears[2]) %>%
  222. mutate(Year = as.Date(paste0(Year, "-01-01")))
  223. return(st)
  224. })
  225. # Within plot ----
  226. output$wiPlot <- renderPlot({
  227. wov <- WiSec()
  228. if (input$wiyears[1] != input$wiyears[2]) {
  229. ggplot(wov, aes(x=Year,
  230. y=Co2eqv, color=Source)) -> w.p
  231. w.p + geom_line(size=1.5, na.rm=TRUE) -> w.p
  232. w.p + theme_linedraw() -> w.p
  233. w.p + scale_color_brewer(type="qual") -> w.p
  234. } else {
  235. ggplot(wov, aes(x=Source,
  236. y=Co2eqv, fill=Source)) -> w.p
  237. w.p + geom_col(na.rm=TRUE) -> w.p
  238. w.p + theme_linedraw() -> w.p
  239. w.p + theme(axis.text.x = element_text(
  240. angle = -30, hjust = 0, vjust = 0)) -> w.p
  241. w.p + scale_fill_brewer(type="qual") -> w.p
  242. }
  243. w.p + guides(linetype=guide_legend(keywidth = 5)) -> w.p
  244. w.p + theme(legend.position = "right",
  245. legend.title = element_blank()) -> w.p
  246. w.p + ylab("CO₂ equivalent (kt)") -> w.p
  247. if (!input$wilegend) {
  248. w.p + theme(legend.position="none") -> w.p
  249. }
  250. mmx <- c(ifelse(input$wiminc, input$wimin, NA),
  251. ifelse(input$wimaxc, input$wimax, NA))
  252. if (input$wiscale == "sqrt(y)") {
  253. w.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
  254. } else if (input$wiscale == "log10(y)") {
  255. w.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
  256. } else {
  257. w.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
  258. }
  259. return(w.p)
  260. })
  261. }
  262. shinyApp(ui = ui, server = server)