我从来没有想过要做到这一点的好方法。我正在尝试创建一个用户函数,用于将RColorBrewer颜色映射到data_frame中的变量级别。我正在绘制igraph中的节点颜色,它需要一个颜色矢量。在得到错误之前,我已经做到了这一点。
library(tidyverse)
library(RColorBrewer)
my_data <- data_frame(
x = sample(c('red','white','foo'),25,T)
)
my_data %>%
group_by(x) %>%
mutate(Blues = brewer.pal(n_distinct(x),'Spectral')) ## this doesn't work
我想要一个有效的功能。如下所示:
function(df,vary){
df %>%
group_by(vary) %>%
mutate(Blues = brewer.pal(n_distinct(vary),'Spectral')) %>%
select(Blues)
}
感谢您的帮助!
答案 0 :(得分:3)
您不需要group_by
,只需使用mutate
并使用brewer.pal(n_distinct(x), 'Spectral')
生成所需的颜色,然后使用match
将颜色映射到变量:
注意:我已使用tidyeval
语法捕获NSE样式的变量,因为我们使用的函数为dplyr
。但如果您愿意,可以选择其他方式编程。 子>
var_to_color <- function(df, var) {
var <- enquo(var)
df %>%
mutate(Colors = brewer.pal(n_distinct(!!var), 'Spectral')[match(!!var, unique(!!var))]) %>%
pull(Colors)
}
var_to_color(my_data, x)
# [1] "#FC8D59" "#FFFFBF" "#FFFFBF" "#FFFFBF" "#99D594" "#FFFFBF" "#99D594"
# [8] "#99D594" "#FC8D59" "#FFFFBF" "#FC8D59" "#FFFFBF" "#FFFFBF" "#99D594"
#[15] "#FC8D59" "#FC8D59" "#99D594" "#FFFFBF" "#FC8D59" "#FC8D59" "#FC8D59"
#[22] "#FC8D59" "#FFFFBF" "#FC8D59" "#FC8D59"
my_data$x
# [1] "white" "red" "red" "red" "foo" "red" "foo" "foo" "white"
#[10] "red" "white" "red" "red" "foo" "white" "white" "foo" "red"
#[19] "white" "white" "white" "white" "red" "white" "white"
不使用dplyr
,一个接受一个向量然后将其映射到颜色的函数可能更有意义:
var_to_color <- function(var) brewer.pal(n_distinct(var), 'Spectral')[match(var, unique(var))]
var_to_color(my_data$x)
# [1] "#FC8D59" "#FFFFBF" "#FFFFBF" "#FFFFBF" "#99D594" "#FFFFBF" "#99D594"
# [8] "#99D594" "#FC8D59" "#FFFFBF" "#FC8D59" "#FFFFBF" "#FFFFBF" "#99D594"
#[15] "#FC8D59" "#FC8D59" "#99D594" "#FFFFBF" "#FC8D59" "#FC8D59" "#FC8D59"
#[22] "#FC8D59" "#FFFFBF" "#FC8D59" "#FC8D59"