Browse Source

Add within sectors tab

Also rename tabs, increase width of sliders
Petra Lamborn 5 years ago
parent
commit
d9061a31ae
1 changed files with 81 additions and 6 deletions
  1. 81
    6
      app.R

+ 81
- 6
app.R View File

@@ -46,15 +46,15 @@ Sector.Fuels <- function(sector) {
46 46
 # UI ----
47 47
 ui <- navbarPage("NZ Emissions", collapsible = TRUE,
48 48
    # Sector tab ----
49
-   tabPanel("By sector",
50
-                h3("Emissions by sector"),
49
+   tabPanel("Between sectors",
50
+                h3("Emissions between sectors"),
51 51
                 plotOutput("secPlot") %>% withSpinner(type=5),
52 52
                 fluidRow(
53 53
                  column(5, offset=1,
54 54
                         h4("Options"),
55 55
                     sliderInput("secyears", "Date range", 
56 56
                       min=1990, max=2010, value=c(1990, 2010),
57
-                      sep="", step=1),
57
+                      sep="", step=1, width="100%"),
58 58
                     selectInput("secscale", "Scale", 
59 59
                       choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
60 60
                     checkboxInput("seclegend", "Show legend", value=TRUE),
@@ -72,16 +72,42 @@ ui <- navbarPage("NZ Emissions", collapsible = TRUE,
72 72
                     )
73 73
                 )             
74 74
         ),
75
+   # Within tab ----
76
+   tabPanel("Within sectors",
77
+            h3("Emissions within sectors"),
78
+            plotOutput("wiPlot") %>% withSpinner(type=5),
79
+            fluidRow(
80
+              column(5, offset=1,
81
+                     h4("Options"),
82
+                    sliderInput("wiyears", "Date range", 
83
+                      min=1990, max=2010, value=c(1990, 2010),
84
+                      sep="", step=1, width="100%"),
85
+                    selectInput("wiscale", "Scale", 
86
+                      choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
87
+                    checkboxInput("wilegend", "Show legend", value=TRUE),
88
+                    checkboxInput("wiminc", "Set minimum", value=FALSE),
89
+                    conditionalPanel("input.wiminc",
90
+                     numericInput("wimin", label=NULL, value=NA)),
91
+                    checkboxInput("wimaxc", "Set maximum", value=FALSE),
92
+                    conditionalPanel("input.wimaxc",
93
+                     numericInput("wimax", label=NULL, value=NA))
94
+                   ),
95
+              column(5, offset=1,
96
+                     h4("Sector"),
97
+                     selectInput("picksec", label=NULL, selected=sectors.a[3], choices=sectors.a)
98
+                     
99
+                   )
100
+            )),
75 101
      # Fuel tab ----
76
-     tabPanel("By fuel",
77
-              h3("Emissions by fuel"),
102
+     tabPanel("Between fuels",
103
+              h3("Emissions between fuels"),
78 104
               plotOutput("fuPlot") %>% withSpinner(type=5),
79 105
                 fluidRow(
80 106
                  column(5, offset=1,
81 107
                         h4("Options"),
82 108
                     sliderInput("fuyears", "Date range", 
83 109
                       min=1990, max=2010, value=c(1990, 2010),
84
-                      sep="", step=1),
110
+                      sep="", step=1, width="100%"),
85 111
                     selectInput("fuscale", "Scale", 
86 112
                       choices=c("y", "sqrt(y)", "log10(y)"), selected="y"),
87 113
                     checkboxInput("fulegend", "Show legend", value=TRUE),
@@ -202,6 +228,55 @@ server <- function(input, output) {
202 228
     }
203 229
     return(f.p)
204 230
   })
231
+  
232
+  # Within logic ----
233
+  WiSec <- reactive({
234
+    st <- gather(Sector.Fuels(input$picksec), key="Year",
235
+                 value="Co2eqv", as.character(1990:2010)) %>% 
236
+      mutate(Year = as.integer(Year), 
237
+             Source = factor(Source, levels=unique(Source))) %>%
238
+    # Logic for filtering out years, fuels here
239
+      filter(Year %in% input$wiyears[1]:input$wiyears[2]) %>%
240
+      mutate(Year = as.Date(paste0(Year, "-01-01")))
241
+    return(st)
242
+  })
243
+  
244
+  # Within plot ----
245
+  output$wiPlot <- renderPlot({
246
+    wov <- WiSec()
247
+    if (input$wiyears[1] != input$wiyears[2]) {
248
+      ggplot(wov, aes(x=Year, 
249
+                      y=Co2eqv, color=Source)) -> w.p
250
+      w.p + geom_line(size=1.5, na.rm=TRUE) -> w.p
251
+      w.p + theme_linedraw() -> w.p
252
+      w.p + scale_color_brewer(type="qual") -> w.p
253
+    } else {
254
+      ggplot(wov, aes(x=Source, 
255
+                      y=Co2eqv, fill=Source)) -> w.p
256
+      w.p + geom_col(na.rm=TRUE) -> w.p
257
+      w.p + theme_linedraw() -> w.p
258
+      w.p + theme(axis.text.x = element_text(
259
+        angle = -30, hjust = 0, vjust = 0)) -> w.p
260
+      w.p + scale_fill_brewer(type="qual") -> w.p
261
+    }
262
+    w.p + guides(linetype=guide_legend(keywidth = 5)) -> w.p
263
+    w.p + theme(legend.position = "right", 
264
+                legend.title = element_blank()) -> w.p
265
+    w.p + ylab("CO₂ equivalent (kt)") -> w.p
266
+    if (!input$wilegend) {
267
+      w.p + theme(legend.position="none") -> w.p
268
+    }
269
+    mmx <- c(ifelse(input$wiminc, input$wimin, NA),
270
+             ifelse(input$wimaxc, input$wimax, NA))
271
+    if (input$wiscale == "sqrt(y)") {
272
+      w.p + scale_y_sqrt(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
273
+    } else if (input$wiscale == "log10(y)") {
274
+      w.p + scale_y_log10(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
275
+    } else {
276
+      w.p + scale_y_continuous(breaks=pretty_breaks(n=5), limits=mmx) -> w.p
277
+    }
278
+    return(w.p)
279
+  })
205 280
 }
206 281
 
207 282
 shinyApp(ui = ui, server = server)