Skip to content

Commit fd29b15

Browse files
author
Tommi Karki
committed
Added equivalent wrappers for 'SurvColors'
1 parent 6d1b06b commit fd29b15

7 files changed

+201
-184
lines changed

EcdcColors.Rproj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
BuildType: Package
16+
PackageUseDevtools: Yes
17+
PackageInstallArgs: --no-multiarch --with-keep.source

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(EcdcColors)
4+
export(EcdcCols)
35
export(SurvColors)
46
export(SurvCols)
57
importFrom("grDevices","rgb")

R/EcdcColors.R

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +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.
12+
#' @author Tommi Karki
13+
#' @keywords colourscales
14+
#' @importFrom "grDevices" "rgb"
15+
#' @export
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/EcdcCols.R

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +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.
12+
#' @author Tommi Karki
13+
#' @keywords colourscales
14+
#' @importFrom "grDevices" "rgb"
15+
#' @export
16+
#' @examples
17+
#' # Select three first green colours
18+
#' EcdcCols("green", n=3)
19+
#'
20+
#' # Select two first qualitative colours
21+
#' EcdcCols("qual", n=2)
22+
#'
23+
#' # Select seven red colours
24+
#' EcdcCols("red", n=7)
25+
#'
26+
#' EcdcCols("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 = EcdcCols(col_scale = "qual", n=2))
33+
#'
34+
#' # Hot-cold colour scale
35+
#' barplot(c(1:4), col = EcdcCols(col_scale = "hotcold", n = 4, hot_cols = 2))
36+
EcdcCols <- 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/SurvCols.R

+2-184
Original file line numberDiff line numberDiff line change
@@ -36,188 +36,6 @@
3636
SurvCols <- function(col_scale = "green", n = NULL, grey_shade = NULL,
3737
hot_cols = NULL){
3838

39-
if(grepl("gray", col_scale)){
40-
col_scale <- "grey"
41-
}
42-
43-
if(grepl("qual", col_scale)){
44-
col_scale <- "qualitative"
45-
}
46-
47-
if(grepl("hot", col_scale)){
48-
col_scale <- "hotcold"
49-
}
50-
51-
if (is.null(n) & col_scale == "hotcold"){
52-
n <- 2
53-
}else if(is.null(n)){
54-
n <- 1
55-
}else{
56-
n <- n
57-
}
58-
59-
if(n < 2 & col_scale == "hotcold"){
60-
n <- 2
61-
}
62-
63-
if(is.null(hot_cols)){
64-
hot_cols <- floor(n/2)
65-
}
66-
67-
if(!is.null(n)){
68-
if(n>7 & col_scale != "qualitative"){
69-
stop("Maximum number of colours (n) for selected colour scale is 7!")
70-
}else if(n>8 & col_scale == "qualitative"){
71-
stop("Maximum number of colours (n) for selected colour scale is 8!")
72-
}}
73-
74-
if(col_scale=="green"){
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))
105-
}
106-
107-
if(col_scale%in%c("blue", "hotcold")){
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))}
139-
}
140-
141-
if(col_scale%in%c("red", "hotcold")){
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))}
173-
}
174-
175-
# greyscale
176-
if(col_scale == "grey"){
177-
if(is.null(grey_shade)){
178-
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')")
179-
grey_shade <- "medium"
180-
}
181-
shades <- c("light",
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-
195-
}
196-
if(col_scale=="qualitative"){
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-
}
208-
if(col_scale=="hotcold"){
209-
if(n<=hot_cols){
210-
stop("Total number (n) of colours must be greater than n of hot colours (hot_cols)!")
211-
}
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-
}
217-
218-
if(!col_scale%in%c("green", "blue", "red", "grey", "qualitative", "hotcold")){
219-
stop("col_scale is not among the currently defined colour palettes,
220-
please select from 'green', 'blue', 'red', 'grey', 'qual(itative)' or 'hot(cold)'")
221-
}
222-
return(cols)
39+
return(SurvColors(col_scale = col_scale, n = n, grey_shade = grey_shade,
40+
hot_cols = hot_cols))
22341
}

man/EcdcColors.Rd

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)