Petra Lamborn 5 years ago
parent
commit
3c236eadbd
1 changed files with 67 additions and 2 deletions
  1. 67
    2
      app.R

+ 67
- 2
app.R View File

@@ -13,6 +13,8 @@ Sector.Tot <- filter(Emissions.Sector, Source == "Total")
13 13
 sectors.a <- unique(Emissions.Sector$Sector)
14 14
 sectors <- unique(Emissions.Sector$Sector)[-1]
15 15
 
16
+fuels <- Emissions.Fuel$Fuel
17
+
16 18
 # gather(comf, key="Year", value="Co2eqv", as.character(1990:2010))
17 19
 
18 20
 Sector.Fuels <- function(sector) {
@@ -28,7 +30,7 @@ Sector.Fuels <- function(sector) {
28 30
 # UI ----
29 31
 ui <- navbarPage("NZ Emissions", collapsible = TRUE,
30 32
    # Sector tab ----
31
-   tabPanel("Sector",
33
+   tabPanel("By sector",
32 34
                 h3("Emissions by sector"),
33 35
                 plotOutput("secPlot"),
34 36
                 fluidRow(
@@ -54,7 +56,33 @@ ui <- navbarPage("NZ Emissions", collapsible = TRUE,
54 56
                     )
55 57
                 )             
56 58
         ),
57
-     tabPanel("By fuel")
59
+     # Fuel tab ----
60
+     tabPanel("By fuel",
61
+              h3("Emissions by fuel"),
62
+              plotOutput("fuPlot"),
63
+                fluidRow(
64
+                 column(5, offset=1,
65
+                        h4("Options"),
66
+                    sliderInput("fuyears", "Date range", 
67
+                      min=1990, max=2010, value=c(1990, 2010),
68
+                      sep="", step=1),
69
+                    selectInput("fuscale", "Scale", 
70
+                      choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
71
+                    checkboxInput("fulegend", "Show legend", value=TRUE),
72
+                    checkboxInput("fuminc", "Set minimum", value=FALSE),
73
+                    conditionalPanel("input.fuminc",
74
+                     numericInput("fumin", label=NULL, value=NA)),
75
+                    checkboxInput("fumaxc", "Set maximum", value=FALSE),
76
+                    conditionalPanel("input.fumaxc",
77
+                     numericInput("fumax", label=NULL, value=NA))
78
+                        ),
79
+                 column(5, offset=1,
80
+                      h4("Fuels"),
81
+                     checkboxGroupInput("fuFuels", NULL, 
82
+                      choices=fuels, selected = fuels)
83
+                    )
84
+                )             
85
+              )
58 86
 )
59 87
 
60 88
 server <- function(input, output) {
@@ -94,6 +122,43 @@ server <- function(input, output) {
94 122
     }
95 123
     return(s.p)
96 124
   })
125
+  
126
+  FuOverall <- reactive({
127
+    st <- gather(Emissions.Fuel, key="Year",
128
+                 value="Co2eqv", as.character(1990:2010)) %>% 
129
+      mutate(Year = as.integer(Year), 
130
+             Fuel = factor(Fuel, levels=unique(Fuel))) %>%
131
+    # Logic for filtering out years, fuels here
132
+      filter(Year %in% input$fuyears[1]:input$fuyears[2], 
133
+             Fuel %in% input$fuFuels) %>%
134
+      mutate(Year = as.Date(paste0(Year, "-01-01")))
135
+    return(st)
136
+  })
137
+  
138
+  output$fuPlot <- renderPlot({
139
+    fov <- FuOverall()
140
+    ggplot(fov, aes(x=Year, 
141
+                    y=Co2eqv, linetype=Fuel, color=Fuel)) -> f.p
142
+    f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
143
+    f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
144
+    f.p + theme_linedraw() -> f.p
145
+    f.p + theme(legend.position = "right", 
146
+                legend.title = element_blank()) -> f.p
147
+    f.p + ylab("CO₂ equivalent (kt)") -> f.p
148
+    if (!input$fulegend) {
149
+      f.p + theme(legend.position="none") -> f.p
150
+    }
151
+    mmx <- c(ifelse(input$fuminc, input$fumin, NA),
152
+             ifelse(input$fumaxc, input$fumax, NA))
153
+    if (input$fuscale == "sqrt(y)") {
154
+      f.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
155
+    } else if (input$fuscale == "log10(y)") {
156
+      f.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
157
+    } else {
158
+      f.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> f.p
159
+    }
160
+    return(f.p)
161
+  })
97 162
 }
98 163
 
99 164
 shinyApp(ui = ui, server = server)