123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- library(shiny)
- library(ggplot2)
- library(scales)
- library(tidyverse)
- library(readxl)
- library(shinycssloaders)
- library(RColorBrewer)
-
- # Original data from
- # https://catalogue.data.govt.nz/dataset/
- # new-zealands-energy-outlook/resource/3caad31e-aa96-4c94-8f54-1000518a0690?inner_span=True
-
- # Data loading ----
- Emissions.Sector <- read_excel("Emissions1990-2010.xlsx",
- sheet = "Sector")
- Emissions.Fuel <- read_excel("Emissions1990-2010.xlsx",
- sheet = "Fuel")
-
- sectors.a <- unique(Emissions.Sector$Sector)
- sectors <- unique(Emissions.Sector$Sector)[-1]
-
- sector.col <- data.frame(Sector=sectors.a,
- col=brewer.pal(length(sectors.a), "Set3"))
- Emissions.Sector <- left_join(Emissions.Sector, sector.col) %>%
- mutate(col = as.character(col))
- Sector.Tot <- filter(Emissions.Sector, Source == "Total")
- Emissions.Fuel <- cbind(Emissions.Fuel,
- col=brewer.pal(nrow(Emissions.Fuel), "Set2")) %>%
- mutate(col=as.character(col))
-
-
- fuels <- Emissions.Fuel$Fuel
-
- # gather(comf, key="Year", value="Co2eqv", as.character(1990:2010))
-
- Sector.Fuels <- function(sector) {
- sf <- filter(Emissions.Sector, Sector == sector)
- if (nrow(sf) > 1) {
- sf <- filter(sf, !grepl("Total", Source))
- }
- return(sf)
- }
-
-
-
- # UI ----
- ui <- navbarPage("NZ Emissions", collapsible = TRUE,
- # Sector tab ----
- tabPanel("Between sectors",
- h3("Emissions between sectors"),
- plotOutput("secPlot") %>% withSpinner(type=5),
- fluidRow(
- column(5, offset=1,
- h4("Options"),
- sliderInput("secyears", "Date range",
- min=1990, max=2010, value=c(1990, 2010),
- sep="", step=1, width="100%"),
- selectInput("secscale", "Scale",
- choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
- checkboxInput("seclegend", "Show legend", value=TRUE),
- checkboxInput("secminc", "Set minimum", value=FALSE),
- conditionalPanel("input.secminc",
- numericInput("secmin", label=NULL, value=NA)),
- checkboxInput("secmaxc", "Set maximum", value=FALSE),
- conditionalPanel("input.secmaxc",
- numericInput("secmax", label=NULL, value=NA))
- ),
- column(5, offset=1,
- h4("Sectors"),
- checkboxGroupInput("secSectors", NULL,
- choices=sectors.a, selected = sectors.a)
- )
- )
- ),
- # Within tab ----
- tabPanel("Within sectors",
- h3("Emissions within sectors"),
- plotOutput("wiPlot") %>% withSpinner(type=5),
- fluidRow(
- column(5, offset=1,
- h4("Options"),
- sliderInput("wiyears", "Date range",
- min=1990, max=2010, value=c(1990, 2010),
- sep="", step=1, width="100%"),
- selectInput("wiscale", "Scale",
- choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
- checkboxInput("wilegend", "Show legend", value=TRUE),
- checkboxInput("wiminc", "Set minimum", value=FALSE),
- conditionalPanel("input.wiminc",
- numericInput("wimin", label=NULL, value=NA)),
- checkboxInput("wimaxc", "Set maximum", value=FALSE),
- conditionalPanel("input.wimaxc",
- numericInput("wimax", label=NULL, value=NA))
- ),
- column(5, offset=1,
- h4("Sector"),
- selectInput("picksec", label=NULL, selected=sectors.a[3], choices=sectors.a)
-
- )
- )),
- # Fuel tab ----
- tabPanel("Between fuels",
- h3("Emissions between fuels"),
- plotOutput("fuPlot") %>% withSpinner(type=5),
- fluidRow(
- column(5, offset=1,
- h4("Options"),
- sliderInput("fuyears", "Date range",
- min=1990, max=2010, value=c(1990, 2010),
- sep="", step=1, width="100%"),
- selectInput("fuscale", "Scale",
- choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
- checkboxInput("fulegend", "Show legend", value=TRUE),
- checkboxInput("fuminc", "Set minimum", value=FALSE),
- conditionalPanel("input.fuminc",
- numericInput("fumin", label=NULL, value=NA)),
- checkboxInput("fumaxc", "Set maximum", value=FALSE),
- conditionalPanel("input.fumaxc",
- numericInput("fumax", label=NULL, value=NA))
- ),
- column(5, offset=1,
- h4("Fuels"),
- checkboxGroupInput("fuFuels", NULL,
- choices=fuels, selected = fuels)
- )
- )
- )
- )
-
- server <- function(input, output) {
-
- # Sector logic ----
- SecOverall <- reactive({
- st <- gather(Sector.Tot, key="Year",
- value="Co2eqv", as.character(1990:2010)) %>%
- mutate(Year = as.integer(Year),
- Sector = factor(Sector, levels=unique(Sector))) %>%
- # Logic for filtering out years, sectors here
- filter(Year %in% input$secyears[1]:input$secyears[2],
- Sector %in% input$secSectors) %>%
- mutate(Year = as.Date(paste0(Year, "-01-01")))
- return(st)
- })
-
- # Sector plot ----
- output$secPlot <- renderPlot({
- sov <- SecOverall()
- if (input$secyears[1] != input$secyears[2]) {
- ggplot(sov, aes(x=Year,
- y=Co2eqv, color=Sector)) -> s.p
- s.p + geom_line(size=1.5, na.rm=TRUE) -> s.p
- s.p + theme_linedraw() -> s.p
- s.p + scale_color_manual(values=unique(sov$col)) -> s.p
- } else {
- ggplot(sov, aes(x=Sector,
- y=Co2eqv, fill=Sector)) -> s.p
- s.p + geom_col(na.rm=TRUE) -> s.p
- s.p + theme_linedraw() -> s.p
- s.p + theme(axis.text.x = element_text(
- angle = -30, hjust = 0, vjust = 0)) -> s.p
- s.p + scale_fill_manual(values=unique(sov$col)) -> s.p
- }
- s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
- s.p + theme(legend.position = "right",
- legend.title = element_blank()) -> s.p
- s.p + ylab("CO₂ equivalent (kt)") -> s.p
- if (!input$seclegend) {
- s.p + theme(legend.position="none") -> s.p
- }
- mmx <- c(ifelse(input$secminc, input$secmin, NA),
- ifelse(input$secmaxc, input$secmax, NA))
- if (input$secscale == "sqrt(y)") {
- s.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> s.p
- } else if (input$secscale == "log10(y)") {
- s.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> s.p
- } else {
- s.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> s.p
- }
- return(s.p)
- })
-
- # Fuel logic ----
- FuOverall <- reactive({
- st <- gather(Emissions.Fuel, key="Year",
- value="Co2eqv", as.character(1990:2010)) %>%
- mutate(Year = as.integer(Year),
- Fuel = factor(Fuel, levels=unique(Fuel))) %>%
- # Logic for filtering out years, fuels here
- filter(Year %in% input$fuyears[1]:input$fuyears[2],
- Fuel %in% input$fuFuels) %>%
- mutate(Year = as.Date(paste0(Year, "-01-01")))
- return(st)
- })
-
- # Fuel plot ----
- output$fuPlot <- renderPlot({
- fov <- FuOverall()
- if (input$fuyears[1] != input$fuyears[2]) {
- ggplot(fov, aes(x=Year,
- y=Co2eqv, color=Fuel)) -> f.p
- f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
- f.p + theme_linedraw() -> f.p
- f.p + scale_color_manual(values=unique(fov$col)) -> f.p
- } else {
- ggplot(fov, aes(x=Fuel,
- y=Co2eqv, fill=Fuel)) -> f.p
- f.p + geom_col(na.rm=TRUE) -> f.p
- f.p + theme_linedraw() -> f.p
- f.p + theme(axis.text.x = element_text(
- angle = -30, hjust = 0, vjust = 0)) -> f.p
- f.p + scale_fill_manual(values=unique(fov$col)) -> f.p
- }
- f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
- f.p + theme(legend.position = "right",
- legend.title = element_blank()) -> f.p
- f.p + ylab("CO₂ equivalent (kt)") -> f.p
- if (!input$fulegend) {
- f.p + theme(legend.position="none") -> f.p
- }
- mmx <- c(ifelse(input$fuminc, input$fumin, NA),
- ifelse(input$fumaxc, input$fumax, NA))
- if (input$fuscale == "sqrt(y)") {
- f.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
- } else if (input$fuscale == "log10(y)") {
- f.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
- } else {
- f.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
- }
- return(f.p)
- })
-
- # Within logic ----
- WiSec <- reactive({
- st <- gather(Sector.Fuels(input$picksec), key="Year",
- value="Co2eqv", as.character(1990:2010)) %>%
- mutate(Year = as.integer(Year),
- Source = factor(Source, levels=unique(Source))) %>%
- # Logic for filtering out years, fuels here
- filter(Year %in% input$wiyears[1]:input$wiyears[2]) %>%
- mutate(Year = as.Date(paste0(Year, "-01-01")))
- return(st)
- })
-
- # Within plot ----
- output$wiPlot <- renderPlot({
- wov <- WiSec()
- if (input$wiyears[1] != input$wiyears[2]) {
- ggplot(wov, aes(x=Year,
- y=Co2eqv, color=Source)) -> w.p
- w.p + geom_line(size=1.5, na.rm=TRUE) -> w.p
- w.p + theme_linedraw() -> w.p
- w.p + scale_color_brewer(type="qual") -> w.p
- } else {
- ggplot(wov, aes(x=Source,
- y=Co2eqv, fill=Source)) -> w.p
- w.p + geom_col(na.rm=TRUE) -> w.p
- w.p + theme_linedraw() -> w.p
- w.p + theme(axis.text.x = element_text(
- angle = -30, hjust = 0, vjust = 0)) -> w.p
- w.p + scale_fill_brewer(type="qual") -> w.p
- }
- w.p + guides(linetype=guide_legend(keywidth = 5)) -> w.p
- w.p + theme(legend.position = "right",
- legend.title = element_blank()) -> w.p
- w.p + ylab("CO₂ equivalent (kt)") -> w.p
- if (!input$wilegend) {
- w.p + theme(legend.position="none") -> w.p
- }
- mmx <- c(ifelse(input$wiminc, input$wimin, NA),
- ifelse(input$wimaxc, input$wimax, NA))
- if (input$wiscale == "sqrt(y)") {
- w.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
- } else if (input$wiscale == "log10(y)") {
- w.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
- } else {
- w.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
- }
- return(w.p)
- })
- }
-
- shinyApp(ui = ui, server = server)
|