-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_colors.R
executable file
·31 lines (26 loc) · 1.38 KB
/
function_colors.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
# Extrend a color palette with DiscretePalette from Seurat
ExtendColorVector = function(color_vector = NULL, target_class, reduce = T, palettes_use = 'glasbey', palette_method = c('RColorBrewer','Seurat'), randomize = F){
missing_classes = setdiff(unique(target_class), names(color_vector))
# Get color and assign
palette_method = match.arg(palette_method)
new_col_all = if(palette_method == 'Seurat') Seurat::DiscretePalette(32, palette = palettes_use) else GetRColorBrewerQual()
# 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'
if(reduce) color_vector = color_vector[unique(target_class)]
}
# Get All Discrete color from RColorBrewer
GetRColorBrewerQual = function(as_list = F){
df = RColorBrewer::brewer.pal.info %>%
filter(category == 'qual') %>%
rownames_to_column('palette')
palettes = df %>% pmap(function(maxcolors, palette, ...){
RColorBrewer::brewer.pal(n = maxcolors, name = palette)
}) %>% setNames(df[['palette']])
if(as_list) return(palettes) else return(unlist(palettes))
}