-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_colors_paletteer.R
40 lines (31 loc) · 1.7 KB
/
function_colors_paletteer.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Paletteer helper
library(paletteer)
# Function parameter = column name : https://stackoverflow.com/questions/62082563/function-to-filter-tibble-with-argument-having-same-name-as-a-column
ShowDiscretePalettes = function(package = 'Redmonder'){
if(!package %in% palettes_d_names$package) stop("package not found. Please use palettes_d_names function to check available ones")
# get all paletter under a package
palettes_d_names %>% filter(package == !!package) %>%
{setNames(pmap(., function(package, palette, ...) str_glue('{package}::{palette}') ), .$palette)} %>% map(paletteer_d)
}
ExtendColorVectorPaletteer = function(color_vector = NULL, target_class, reduce = T, colors_use=NULL, randomize = F){
missing_classes = setdiff(unique(target_class), names(color_vector))
# Get color
new_col_all = if(is.null(colors_use)){
c(paletteer_d('Redmonder::qMSOBuWarm'),
#paletteer_d("Redmonder::qMSO12")[-2:0], # remove first 2 colors
#paletteer_d("Redmonder::qMSO15")[-2:0], # remove first colors
paletteer_d("Redmonder::qMSOGn")[-2:0], # remove first 2 colors
paletteer_d("Redmonder::qMSORdOr")[-2:0], # remove first 2 colors
paletteer_d("Redmonder::qMSORdPu")[-2:0] # remove first 2 colors
)
}else{ colors_use }
# rm duplicated color
new_col_use = setdiff(new_col_all, color_vector) # remove exisiting color
# Choose color
new_col_use = if(randomize) sample(new_col_use,length(missing_classes)) else new_col_use[1:length(missing_classes)]
new_col = new_col_use %>% setNames(missing_classes)
# Combine
color_vector = c(color_vector, new_col)
# Remove 'Non exisiting item'toc)
if(reduce) color_vector = color_vector[unique(target_class)]
}