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
 )
91
 )
92
 
92
 
93
 server <- function(input, output) {
93
 server <- function(input, output) {
94
+  
95
+  # Sector logic ----
94
   SecOverall <- reactive({
96
   SecOverall <- reactive({
95
     st <- gather(Sector.Tot, key="Year",
97
     st <- gather(Sector.Tot, key="Year",
96
                  value="Co2eqv", as.character(1990:2010)) %>% 
98
                  value="Co2eqv", as.character(1990:2010)) %>% 
103
     return(st)
105
     return(st)
104
   })
106
   })
105
    
107
    
108
+  # Sector plot ----
106
   output$secPlot <- renderPlot({
109
   output$secPlot <- renderPlot({
107
     sov <- SecOverall()
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
     s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
124
     s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
112
-    s.p + theme_linedraw() -> s.p
113
     s.p + theme(legend.position = "right", 
125
     s.p + theme(legend.position = "right", 
114
                 legend.title = element_blank()) -> s.p
126
                 legend.title = element_blank()) -> s.p
115
     s.p + ylab("CO₂ equivalent (kt)") -> s.p
127
     s.p + ylab("CO₂ equivalent (kt)") -> s.p
128
     return(s.p)
140
     return(s.p)
129
   })
141
   })
130
   
142
   
143
+  # Fuel logic ----
131
   FuOverall <- reactive({
144
   FuOverall <- reactive({
132
     st <- gather(Emissions.Fuel, key="Year",
145
     st <- gather(Emissions.Fuel, key="Year",
133
                  value="Co2eqv", as.character(1990:2010)) %>% 
146
                  value="Co2eqv", as.character(1990:2010)) %>% 
140
     return(st)
153
     return(st)
141
   })
154
   })
142
   
155
   
156
+  # Fuel plot ----
143
   output$fuPlot <- renderPlot({
157
   output$fuPlot <- renderPlot({
144
     fov <- FuOverall()
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
     f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
172
     f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
149
-    f.p + theme_linedraw() -> f.p
150
     f.p + theme(legend.position = "right", 
173
     f.p + theme(legend.position = "right", 
151
                 legend.title = element_blank()) -> f.p
174
                 legend.title = element_blank()) -> f.p
152
     f.p + ylab("CO₂ equivalent (kt)") -> f.p
175
     f.p + ylab("CO₂ equivalent (kt)") -> f.p