Skip to content

Commit 4ef9f98

Browse files
committed
Revert "Merge pull request #2 from mariabnd/dev"
This reverts commit 4dcde80, reversing changes made to 37033a2.
1 parent 55f2d25 commit 4ef9f98

File tree

2 files changed

+172
-132
lines changed

2 files changed

+172
-132
lines changed

R/EcdcColors.R

+39-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1+
#' Colour palettes following the March 2018 ECDC guidelines for presentation of surveillance data
2+
#'
3+
#' Full document: European Centre for Disease Prevention and Control. Guidelines for presentation of surveillance data.
4+
#' Stockholm: ECDC; 2018. Available from: \href{https://ecdc.europa.eu/en/publications-data/guidelines-presentation-surveillance-data}{Guidelines for presentation of surveillance data}
5+
#'
6+
#' @param col_scale Selected colour scale, defaults to 'green'. Select from 'green', 'blue', 'red', 'grey', 'qual(itative)' or 'hot(cold)'
7+
#' @param n Number of colours from each colour scale, apart from grey, in order indicated in the guidelines. Defaults to one colour, apart from two colours for the hotcold scale,
8+
#' max 7-8 colours for each scale. To select grey shades, use the argument grey_shade; to select number of hot (warm) colours in the hotcold scale, use the argument hot_cols.
9+
#' @param grey_shade Only used with grey colours, defaults to 'medium'. Selected shade(s) of grey in selected order; c('light', 'mediumlight','medium','mediumdark','dark'). Overrides given number of colours (n).
10+
#' @param hot_cols Selected number of hot (warm) colours in the hotcold colour scale, use only for hotcold. Must be smaller than the total number of colours (n).
11+
#' Defaults to floored half of total hotcold colours.
112
#' @author Tommi Karki
13+
#' @keywords colourscales
14+
#' @importFrom "grDevices" "rgb"
215
#' @export
3-
#' @rdname SurvColors
4-
#' @usage NULL
5-
EcdcColors <- SurvColors
6-
7-
#' @author Tommi Karki
8-
#' @export
9-
#' @rdname SurvColors
10-
#' @usage NULL
11-
EcdcColours <- SurvColors
12-
13-
#' @author Tommi Karki
14-
#' @export
15-
#' @rdname SurvColors
16-
#' @usage NULL
17-
ECDCColours <- SurvColors
18-
19-
#' @author Tommi Karki
20-
#' @export
21-
#' @rdname SurvColors
22-
#' @usage NULL
23-
ECDCColors <- SurvColors
16+
#' @examples
17+
#' # Select three first green colours
18+
#' EcdcColors("green", n=3)
19+
#'
20+
#' # Select two first qualitative colours
21+
#' EcdcColors("qual", n=2)
22+
#'
23+
#' # Select seven red colours
24+
#' EcdcColors("red", n=7)
25+
#'
26+
#' EcdcColors("grey", grey_shade = c("mediumlight", "dark"))
27+
#'
28+
#' # Use in a graph
29+
#' # Dummy data
30+
#' mydat <- data.frame(ID = c(seq(1,10,1)),
31+
#' Gender = c(rep(c("F", "M"),5)))
32+
#' barplot(table(mydat$Gender), col = EcdcColors(col_scale = "qual", n=2))
33+
#'
34+
#' # Hot-cold colour scale
35+
#' barplot(c(1:4), col = EcdcColors(col_scale = "hotcold", n = 4, hot_cols = 2))
36+
EcdcColors <- function(col_scale = "green", n = NULL, grey_shade = NULL,
37+
hot_cols = NULL){
38+
39+
return(SurvColors(col_scale = col_scale, n = n, grey_shade = grey_shade,
40+
hot_cols = hot_cols))
41+
}

R/SurvColors.R

+133-111
Original file line numberDiff line numberDiff line change
@@ -65,137 +65,159 @@ SurvColors <- function(col_scale = "green", n = NULL, grey_shade = NULL,
6565
}
6666

6767
if(!is.null(n)){
68-
if(n>7 & !col_scale %in% c("qualitative", "hotcold")){
69-
stop("Maximum number of colours (n) for selected colour scale is 7!")
70-
}else if(n>8 & col_scale %in% c("qualitative", "hotcold")){
71-
stop("Maximum number of colours (n) for selected colour scale is 8!")
72-
}}
68+
if(n>7 & !col_scale %in% c("qualitative", "hotcold")){
69+
stop("Maximum number of colours (n) for selected colour scale is 7!")
70+
}else if(n>8 & col_scale %in% c("qualitative", "hotcold")){
71+
stop("Maximum number of colours (n) for selected colour scale is 8!")
72+
}}
7373

7474
if(col_scale=="green"){
75-
# greens
76-
opts <- c(rgb(12,72,40, maxColorValue = 255),
77-
rgb(26,110,49, maxColorValue = 255),
78-
rgb(32,119,50, maxColorValue = 255),
79-
rgb(32,129,53, maxColorValue = 255),
80-
rgb(40,147,55, maxColorValue = 255),
81-
rgb(66,158,53, maxColorValue = 255),
82-
rgb(101,179,46, maxColorValue = 255),
83-
rgb(137,190,71, maxColorValue = 255),
84-
rgb(156,198,90, maxColorValue = 255),
85-
rgb(178,207,110, maxColorValue = 255),
86-
rgb(192,212,122, maxColorValue = 255),
87-
rgb(201,217,113, maxColorValue = 255),
88-
rgb(231,231,185, maxColorValue = 255))
75+
# greens
76+
gscale1 <- rgb(101,179,46, maxColorValue = 255)
77+
gscale2 <- c(rgb(32,119,50, maxColorValue = 255),
78+
rgb(178,207,110, maxColorValue = 255))
79+
gscale3 <- c(rgb(26,110,49, maxColorValue = 255),
80+
rgb(101,179,46, maxColorValue = 255),
81+
rgb(201,217,113, maxColorValue = 255))
82+
gscale4 <- c(rgb(26,110,49, maxColorValue = 255),
83+
rgb(40,147,55, maxColorValue = 255),
84+
rgb(156,198,90, maxColorValue = 255),
85+
rgb(201,217,113, maxColorValue = 255))
86+
gscale5 <- c(rgb(12,72,40, maxColorValue = 255),
87+
rgb(32,129,53, maxColorValue = 255),
88+
rgb(101,179,46, maxColorValue = 255),
89+
rgb(178,207,110, maxColorValue = 255),
90+
rgb(231,231,185, maxColorValue = 255))
91+
gscale6 <- c(rgb(12,72,40, maxColorValue = 255),
92+
rgb(32,119,50, maxColorValue = 255),
93+
rgb(66,158,53, maxColorValue = 255),
94+
rgb(137,190,71, maxColorValue = 255),
95+
rgb(192,212,122, maxColorValue = 255),
96+
rgb(231,231,185, maxColorValue = 255))
97+
gscale7 <- c(rgb(12,72,40, maxColorValue = 255),
98+
rgb(26,110,49, maxColorValue = 255),
99+
rgb(40,147,55, maxColorValue = 255),
100+
rgb(101,179,46, maxColorValue = 255),
101+
rgb(156,198,90, maxColorValue = 255),
102+
rgb(201,217,113, maxColorValue = 255),
103+
rgb(231,231,185, maxColorValue = 255))
104+
cols <- get(paste0("gscale", n))
89105
}
90106

91107
if(col_scale%in%c("blue", "hotcold")){
92-
# blues
93-
opts <- c(rgb(0,60,80, maxColorValue = 255),
94-
rgb(26,107,133, maxColorValue = 255),
95-
rgb(39,117,142, maxColorValue = 255),
96-
rgb(60,142,162, maxColorValue = 255),
97-
rgb(73,153,171, maxColorValue = 255),
98-
rgb(95,167,181, maxColorValue = 255),
99-
rgb(124,189,196, maxColorValue = 255),
100-
rgb(147,199,207, maxColorValue = 255),
101-
rgb(165,206,215, maxColorValue = 255),
102-
rgb(173,210,221, maxColorValue = 255),
103-
rgb(187,216,229, maxColorValue = 255),
104-
rgb(194,218,232, maxColorValue = 255),
105-
rgb(227,232,240, maxColorValue = 255))
106-
}
107-
108-
if(col_scale %in% c("blue", "green"){
109-
scale1 <- opts[7]
110-
scale2 <- opts[c(4, 11)]
111-
scale3 <- opts[c(2, 7, 12)]
112-
scale4 <- opts[c(2, 5, 9, 12)]
113-
scale5 <- opts[c(1, 4, 10, 13)]
114-
scale6 <- opts[c(1, 3, 6, 8, 11, 13)]
115-
scale7 <- opts[c(1, 2, 5, 7, 9, 12, 13)]
116-
117-
cols <- get(paste0("scale", n))
108+
# blues
109+
bscale1 <- rgb(124,189,196, maxColorValue = 255)
110+
bscale2 <- c(rgb(60,142,162, maxColorValue = 255),
111+
rgb(173,210,221, maxColorValue = 255))
112+
bscale3 <- c(rgb(26,107,133, maxColorValue = 255),
113+
rgb(124,189,196, maxColorValue = 255),
114+
rgb(194,218,232, maxColorValue = 255))
115+
bscale4 <- c(rgb(26,107,133, maxColorValue = 255),
116+
rgb(73,153,171, maxColorValue = 255),
117+
rgb(165,206,215, maxColorValue = 255),
118+
rgb(194,218,232, maxColorValue = 255))
119+
bscale5 <- c(rgb(0,60,80, maxColorValue = 255),
120+
rgb(60,142,162, maxColorValue = 255),
121+
rgb(124,189,196, maxColorValue = 255),
122+
rgb(173,210,221, maxColorValue = 255),
123+
rgb(227,232,240, maxColorValue = 255))
124+
bscale6 <- c(rgb(0,60,80, maxColorValue = 255),
125+
rgb(39,117,142, maxColorValue = 255),
126+
rgb(95,167,181, maxColorValue = 255),
127+
rgb(147,199,207, maxColorValue = 255),
128+
rgb(187,216,229, maxColorValue = 255),
129+
rgb(227,232,240, maxColorValue = 255))
130+
bscale7 <- c(rgb(0,60,80, maxColorValue = 255),
131+
rgb(26,107,133, maxColorValue = 255),
132+
rgb(73,153,171, maxColorValue = 255),
133+
rgb(124,189,196, maxColorValue = 255),
134+
rgb(165,206,215, maxColorValue = 255),
135+
rgb(194,218,232, maxColorValue = 255),
136+
rgb(227,232,240, maxColorValue = 255))
137+
if(col_scale=="blue"){
138+
cols <- get(paste0("bscale", n))}
118139
}
119140

120141
if(col_scale%in%c("red", "hotcold")){
121-
# reds
122-
opts <- c(rgb(124,23,15, maxColorValue = 255),
123-
rgb(168,45,23, maxColorValue = 255),
124-
rgb(174,52,23, maxColorValue = 255),
125-
rgb(182,61,23, maxColorValue = 255),
126-
rgb(195,74,23, maxColorValue = 255),
127-
rgb(199,79,27, maxColorValue = 255),
128-
rgb(204,107,33, maxColorValue = 255),
129-
rgb(214,133,43, maxColorValue = 255),
130-
rgb(220,150,53, maxColorValue = 255),
131-
rgb(225,167,68, maxColorValue = 255),
132-
rgb(230,176,77, maxColorValue = 255),
133-
rgb(233,184,85, maxColorValue = 255),
134-
rgb(241,214,118, maxColorValue = 255))
135-
136-
rscale1 <- opts[2]
137-
rscale2 <- opts[c(2, 10)]
138-
rscale3 <- opts[c(2, 7, 12)]
139-
rscale4 <- opts[c(2, 5, 9, 12)]
140-
rscale5 <- opts[c(1, 4, 10, 13)]
141-
rscale6 <- opts[c(1, 3, 6, 8, 11, 13)]
142-
rscale7 <- opts[c(1, 2, 5, 7, 9, 12, 13)]
143-
if(col_scale=="red"){
144-
cols <- get(paste0("rscale", n))}
142+
# reds
143+
rscale1 <- rgb(168,45,23, maxColorValue = 255)
144+
rscale2 <- c(rgb(168,45,23, maxColorValue = 255),
145+
rgb(225,167,68, maxColorValue = 255))
146+
rscale3 <- c(rgb(168,45,23, maxColorValue = 255),
147+
rgb(204,107,33, maxColorValue = 255),
148+
rgb(233,184,85, maxColorValue = 255))
149+
rscale4 <- c(rgb(168,45,23, maxColorValue = 255),
150+
rgb(195,74,23, maxColorValue = 255),
151+
rgb(220,150,53, maxColorValue = 255),
152+
rgb(233,184,85, maxColorValue = 255))
153+
rscale5 <- c(rgb(124,23,15, maxColorValue = 255),
154+
rgb(182,61,23, maxColorValue = 255),
155+
rgb(204,107,33, maxColorValue = 255),
156+
rgb(225,167,68, maxColorValue = 255),
157+
rgb(241,214,118, maxColorValue = 255))
158+
rscale6 <- c(rgb(124,23,15, maxColorValue = 255),
159+
rgb(174,52,23, maxColorValue = 255),
160+
rgb(199,79,27, maxColorValue = 255),
161+
rgb(214,133,43, maxColorValue = 255),
162+
rgb(230,176,77, maxColorValue = 255),
163+
rgb(241,214,118, maxColorValue = 255))
164+
rscale7 <- c(rgb(124,23,15, maxColorValue = 255),
165+
rgb(168,45,23, maxColorValue = 255),
166+
rgb(195,74,23, maxColorValue = 255),
167+
rgb(204,107,33, maxColorValue = 255),
168+
rgb(220,150,53, maxColorValue = 255),
169+
rgb(233,184,85, maxColorValue = 255),
170+
rgb(241,214,118, maxColorValue = 255))
171+
if(col_scale=="red"){
172+
cols <- get(paste0("rscale", n))}
145173
}
146-
147-
# greyscale
174+
175+
# greyscale
148176
if(col_scale == "grey"){
149177
if(is.null(grey_shade)){
150178
message("Greyzone - number of colours defined by shades of grey, defaults to 'medium'. If you want specific grey shade(s) in specific order, please insert the grey_shade(s): c('light', 'mediumlight','medium','mediumdark','dark')")
151179
grey_shade <- "medium"
152-
}
180+
}
153181
shades <- c("light",
154-
"mediumlight",
155-
"medium",
156-
"mediumdark",
157-
"dark")
158-
cols <- c(rgb(229,229,229, maxColorValue = 255),
159-
rgb(199,199,199, maxColorValue = 255),
160-
rgb(128,128,128, maxColorValue = 255),
161-
rgb(113,113,113, maxColorValue = 255),
162-
rgb(63,63,63, maxColorValue = 255))
163-
cols <- cols[shades %in% grey_shade]
164-
shades <- shades[shades %in% grey_shade]
165-
cols <- cols[order(match(shades, grey_shade))]
166-
182+
"mediumlight",
183+
"medium",
184+
"mediumdark",
185+
"dark")
186+
cols <- c(rgb(229,229,229, maxColorValue = 255),
187+
rgb(199,199,199, maxColorValue = 255),
188+
rgb(128,128,128, maxColorValue = 255),
189+
rgb(113,113,113, maxColorValue = 255),
190+
rgb(63,63,63, maxColorValue = 255))
191+
cols <- cols[shades %in% grey_shade]
192+
shades <- shades[shades %in% grey_shade]
193+
cols <- cols[order(match(shades, grey_shade))]
194+
167195
}
168196
if(col_scale=="qualitative"){
169-
# qualitative colours
170-
cols <- c(rgb(101,179,46, maxColorValue = 255),
171-
rgb(124,189,196, maxColorValue = 255),
172-
rgb(192,210,54, maxColorValue = 255),
173-
rgb(62,91,132, maxColorValue = 255),
174-
rgb(0,140,117, maxColorValue = 255),
175-
rgb(130,66,141, maxColorValue = 255),
176-
rgb(232,104,63, maxColorValue = 255),
177-
rgb(184,26,93, maxColorValue = 255))
178-
cols <- cols[1:n]
179-
}
197+
# qualitative colours
198+
cols <- c(rgb(101,179,46, maxColorValue = 255),
199+
rgb(124,189,196, maxColorValue = 255),
200+
rgb(192,210,54, maxColorValue = 255),
201+
rgb(62,91,132, maxColorValue = 255),
202+
rgb(0,140,117, maxColorValue = 255),
203+
rgb(130,66,141, maxColorValue = 255),
204+
rgb(232,104,63, maxColorValue = 255),
205+
rgb(184,26,93, maxColorValue = 255))
206+
cols <- cols[1:n]
207+
}
180208
if(col_scale=="hotcold"){
181-
if(n<=hot_cols){
182-
stop("Total number (n) of colours must be greater than n of hot colours (hot_cols)!")
183-
}
184-
cold_cols <- n-hot_cols
185-
cols <- get(paste0("rscale", hot_cols))
186-
cols <- c(cols, rev(get(paste0("bscale", cold_cols))))
187-
209+
if(n<=hot_cols){
210+
stop("Total number (n) of colours must be greater than n of hot colours (hot_cols)!")
188211
}
212+
cold_cols <- n-hot_cols
213+
cols <- get(paste0("rscale", hot_cols))
214+
cols <- c(cols, rev(get(paste0("bscale", cold_cols))))
215+
216+
}
189217

190218
if(!col_scale%in%c("green", "blue", "red", "grey", "qualitative", "hotcold")){
191-
stop("col_scale is not among the currently defined colour palettes,
219+
stop("col_scale is not among the currently defined colour palettes,
192220
please select from 'green', 'blue', 'red', 'grey', 'qual(itative)' or 'hot(cold)'")
193-
}
221+
}
194222
return(cols)
195223
}
196-
197-
#' @author Tommi Karki
198-
#' @export
199-
#' @rdname SurvColors
200-
#' @usage NULL
201-
SurvColours <- SurvColors

0 commit comments

Comments
 (0)