Browse Source

Bar graphs for single years

Also better code annotation
Petra Lamborn 5 years ago
parent
commit
d1c991b89f
1 changed files with 31 additions and 8 deletions
  1. 31
    8
      app.R

+ 31
- 8
app.R View File

@@ -91,6 +91,8 @@ ui <- navbarPage("NZ Emissions", collapsible = TRUE,
91 91
 )
92 92
 
93 93
 server <- function(input, output) {
94
+  
95
+  # Sector logic ----
94 96
   SecOverall <- reactive({
95 97
     st <- gather(Sector.Tot, key="Year",
96 98
                  value="Co2eqv", as.character(1990:2010)) %>% 
@@ -103,13 +105,23 @@ server <- function(input, output) {
103 105
     return(st)
104 106
   })
105 107
    
108
+  # Sector plot ----
106 109
   output$secPlot <- renderPlot({
107 110
     sov <- SecOverall()
108
-    ggplot(sov, aes(x=Year, 
109
-                    y=Co2eqv, linetype=Sector, color=Sector)) -> s.p
110
-    s.p + geom_line(size=1.5, na.rm=TRUE) -> s.p
111
+    if (input$secyears[1] != input$secyears[2]) {
112
+      ggplot(sov, aes(x=Year, 
113
+                      y=Co2eqv, linetype=Sector, color=Sector)) -> s.p
114
+      s.p + geom_line(size=1.5, na.rm=TRUE) -> s.p
115
+      s.p + theme_linedraw() -> s.p
116
+    } else {
117
+      ggplot(sov, aes(x=Sector, 
118
+                      y=Co2eqv, fill=Sector)) -> s.p
119
+      s.p + geom_col(na.rm=TRUE) -> s.p
120
+      s.p + theme_linedraw() -> s.p
121
+      s.p + theme(axis.text.x = element_text(
122
+        angle = -30, hjust = 0, vjust = 0)) -> s.p
123
+    }
111 124
     s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
112
-    s.p + theme_linedraw() -> s.p
113 125
     s.p + theme(legend.position = "right", 
114 126
                 legend.title = element_blank()) -> s.p
115 127
     s.p + ylab("CO₂ equivalent (kt)") -> s.p
@@ -128,6 +140,7 @@ server <- function(input, output) {
128 140
     return(s.p)
129 141
   })
130 142
   
143
+  # Fuel logic ----
131 144
   FuOverall <- reactive({
132 145
     st <- gather(Emissions.Fuel, key="Year",
133 146
                  value="Co2eqv", as.character(1990:2010)) %>% 
@@ -140,13 +153,23 @@ server <- function(input, output) {
140 153
     return(st)
141 154
   })
142 155
   
156
+  # Fuel plot ----
143 157
   output$fuPlot <- renderPlot({
144 158
     fov <- FuOverall()
145
-    ggplot(fov, aes(x=Year, 
146
-                    y=Co2eqv, linetype=Fuel, color=Fuel)) -> f.p
147
-    f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
159
+    if (input$fuyears[1] != input$fuyears[2]) {
160
+      ggplot(fov, aes(x=Year, 
161
+                      y=Co2eqv, linetype=Fuel, color=Fuel)) -> f.p
162
+      f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
163
+      f.p + theme_linedraw() -> f.p
164
+    } else {
165
+      ggplot(fov, aes(x=Fuel, 
166
+                      y=Co2eqv, fill=Fuel)) -> f.p
167
+      f.p + geom_col(na.rm=TRUE) -> f.p
168
+      f.p + theme_linedraw() -> f.p
169
+      f.p + theme(axis.text.x = element_text(
170
+        angle = -30, hjust = 0, vjust = 0)) -> f.p
171
+    }
148 172
     f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
149
-    f.p + theme_linedraw() -> f.p
150 173
     f.p + theme(legend.position = "right", 
151 174
                 legend.title = element_blank()) -> f.p
152 175
     f.p + ylab("CO₂ equivalent (kt)") -> f.p