Browse Source

Static colorbrewer colours

Petra Lamborn 5 years ago
parent
commit
b45dd0651a
1 changed files with 15 additions and 2 deletions
  1. 15
    2
      app.R

+ 15
- 2
app.R View File

4
 library(tidyverse)
4
 library(tidyverse)
5
 library(readxl)
5
 library(readxl)
6
 library(shinycssloaders)
6
 library(shinycssloaders)
7
+library(RColorBrewer)
7
 
8
 
8
 # Original data from 
9
 # Original data from 
9
 # https://catalogue.data.govt.nz/dataset/
10
 # https://catalogue.data.govt.nz/dataset/
10
 # new-zealands-energy-outlook/resource/3caad31e-aa96-4c94-8f54-1000518a0690?inner_span=True
11
 # new-zealands-energy-outlook/resource/3caad31e-aa96-4c94-8f54-1000518a0690?inner_span=True
11
 
12
 
13
+# Data loading ----
12
 Emissions.Sector <- read_excel("Emissions1990-2010.xlsx", 
14
 Emissions.Sector <- read_excel("Emissions1990-2010.xlsx", 
13
                                         sheet = "Sector")
15
                                         sheet = "Sector")
14
 Emissions.Fuel <- read_excel("Emissions1990-2010.xlsx", 
16
 Emissions.Fuel <- read_excel("Emissions1990-2010.xlsx", 
15
                              sheet = "Fuel")
17
                              sheet = "Fuel")
18
+sector.col <- data.frame(Sector=sectors.a, 
19
+                         col=brewer.pal(length(sectors.a), "Set3"))
20
+Emissions.Sector <- left_join(Emissions.Sector, sector.col) %>% 
21
+  mutate(col = as.character(col))
16
 Sector.Tot <- filter(Emissions.Sector, Source == "Total")
22
 Sector.Tot <- filter(Emissions.Sector, Source == "Total")
23
+Emissions.Fuel <- cbind(Emissions.Fuel, 
24
+                        col=brewer.pal(nrow(Emissions.Fuel), "Set2")) %>%
25
+  mutate(col=as.character(col))
17
 
26
 
18
 sectors.a <- unique(Emissions.Sector$Sector)
27
 sectors.a <- unique(Emissions.Sector$Sector)
19
 sectors <- unique(Emissions.Sector$Sector)[-1]
28
 sectors <- unique(Emissions.Sector$Sector)[-1]
110
     sov <- SecOverall()
119
     sov <- SecOverall()
111
     if (input$secyears[1] != input$secyears[2]) {
120
     if (input$secyears[1] != input$secyears[2]) {
112
       ggplot(sov, aes(x=Year, 
121
       ggplot(sov, aes(x=Year, 
113
-                      y=Co2eqv, linetype=Sector, color=Sector)) -> s.p
122
+                      y=Co2eqv, color=Sector)) -> s.p
114
       s.p + geom_line(size=1.5, na.rm=TRUE) -> s.p
123
       s.p + geom_line(size=1.5, na.rm=TRUE) -> s.p
115
       s.p + theme_linedraw() -> s.p
124
       s.p + theme_linedraw() -> s.p
125
+      s.p + scale_color_manual(values=unique(sov$col)) -> s.p
116
     } else {
126
     } else {
117
       ggplot(sov, aes(x=Sector, 
127
       ggplot(sov, aes(x=Sector, 
118
                       y=Co2eqv, fill=Sector)) -> s.p
128
                       y=Co2eqv, fill=Sector)) -> s.p
120
       s.p + theme_linedraw() -> s.p
130
       s.p + theme_linedraw() -> s.p
121
       s.p + theme(axis.text.x = element_text(
131
       s.p + theme(axis.text.x = element_text(
122
         angle = -30, hjust = 0, vjust = 0)) -> s.p
132
         angle = -30, hjust = 0, vjust = 0)) -> s.p
133
+      s.p + scale_fill_manual(values=unique(sov$col)) -> s.p
123
     }
134
     }
124
     s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
135
     s.p + guides(linetype=guide_legend(keywidth = 5)) -> s.p
125
     s.p + theme(legend.position = "right", 
136
     s.p + theme(legend.position = "right", 
158
     fov <- FuOverall()
169
     fov <- FuOverall()
159
     if (input$fuyears[1] != input$fuyears[2]) {
170
     if (input$fuyears[1] != input$fuyears[2]) {
160
       ggplot(fov, aes(x=Year, 
171
       ggplot(fov, aes(x=Year, 
161
-                      y=Co2eqv, linetype=Fuel, color=Fuel)) -> f.p
172
+                      y=Co2eqv, color=Fuel)) -> f.p
162
       f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
173
       f.p + geom_line(size=1.5, na.rm=TRUE) -> f.p
163
       f.p + theme_linedraw() -> f.p
174
       f.p + theme_linedraw() -> f.p
175
+      f.p + scale_color_manual(values=unique(fov$col)) -> f.p
164
     } else {
176
     } else {
165
       ggplot(fov, aes(x=Fuel, 
177
       ggplot(fov, aes(x=Fuel, 
166
                       y=Co2eqv, fill=Fuel)) -> f.p
178
                       y=Co2eqv, fill=Fuel)) -> f.p
168
       f.p + theme_linedraw() -> f.p
180
       f.p + theme_linedraw() -> f.p
169
       f.p + theme(axis.text.x = element_text(
181
       f.p + theme(axis.text.x = element_text(
170
         angle = -30, hjust = 0, vjust = 0)) -> f.p
182
         angle = -30, hjust = 0, vjust = 0)) -> f.p
183
+      f.p + scale_fill_manual(values=unique(fov$col)) -> f.p
171
     }
184
     }
172
     f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
185
     f.p + guides(linetype=guide_legend(keywidth = 5)) -> f.p
173
     f.p + theme(legend.position = "right", 
186
     f.p + theme(legend.position = "right",