设置具有不同 "parent" 级别的调色板?
Setting color palettes with different "parent" levels?
我正在尝试使用看起来像这样的数据在带有传单包的地图中着色:
+--------+---------+-------+
| region | terrain | sales |
+--------+---------+-------+
| 1 | 1A | 253 |
| 1 | 1B | 280 |
| 1 | 1C | 360 |
| 1 | 1D | 350 |
| 1 | 1E | 335 |
| 1 | 1F | 275 |
| 2 | 2A | 200 |
| 2 | 2B | 300 |
| 2 | 2C | 400 |
| 2 | 2D | 250 |
| 2 | 2E | 350 |
+--------+---------+-------+
虽然我知道如何根据一个变量创建调色板,但我想根据每个变量创建一个调色板"parent"
所以假设我想要 1 = "Reds"
和 2 = "Blues"
但是在这两种 "parent" 颜色中,我希望这些区域内的所有地形都根据销售情况进行着色?
这没有明确使用 leaflet
,而是使用 RColorBrewer
(传单导入)
此函数将按给定的第一列拆分数据框,并依次为第二列中的每个值赋予每个 Pals 的颜色。
最后我将其分配给了一个列,但您可能希望以不同的方式使用它。
library(RColorBrewer)
library(dplyr)
createCustomPalette <- function(df, parentCol = "region",subCol = "terrain",
Pals = c("Blues", "Reds", "Greens", "Greys", "Oranges", "Purples"),
returnDF = FALSE){
Parents <- as.numeric(unique(df[[parentCol]]))
ParCols <- lapply(seq_along(Parents), function(x)
RColorBrewer::brewer.pal(length(unique(df[df[parentCol]==Parents[x],subCol])), Pals[x]))
Cols <- unlist(ParCols)
Colsdf <- data.frame(Col = Cols, sub = unlist(lapply(Parents, function(x) unique(df[df[[parentCol]]==x,][[subCol]]))),
stringsAsFactors = F)
outdf <- dplyr::right_join(Colsdf,df, by = c("sub"=subCol))
# outdf <- merge(df, Colsdf, by.x = subCol, by.y = "sub", all.x = T, sort = FALSE )
if(returnDF){
return(outdf)
}else{
return(outdf$Col)
}
}
mydf$Cols <- createCustomPalette(mydf)
scales::show_col(mydf$Cols)
我尽量避免使用 dplyr
,因为我不想添加新的库,但是合并和维护行顺序(如果您只是获取颜色,这很重要)在 base R 中是一个痛苦。如果你不想使用 dplyr
你只需要添加行号并合并然后排序。
您可以使用library(colourvalues)
为向量/数据列生成特定颜色
设置数据
## using a known / reproducible data object
df <- mapdeck::capitals
## making a 'region' column to represent your data
df$region <- ifelse( tolower( substr(df$country,1,1) ) %in% letters[1:13], 1, 2 )
## a random 'sales' column
df$sales <- rnorm(n = nrow(df) )
着色
library(colourvalues)
## order the data first so we can simply `<- c()` the result
df <- df[with(df, order(region)), ]
region1 <- colour_values( x = df[ df$region == 1, "sales"], palette = "reds" )
region2 <- colour_values( x = df[ df$region == 2, "sales"], palette = "blues")
df$colours <- c(region1, region2)
绘图
library(leaflet)
leaflet() %>%
addTiles() %>%
addCircles(
data = df
, color = df$colours
)
我正在尝试使用看起来像这样的数据在带有传单包的地图中着色:
+--------+---------+-------+
| region | terrain | sales |
+--------+---------+-------+
| 1 | 1A | 253 |
| 1 | 1B | 280 |
| 1 | 1C | 360 |
| 1 | 1D | 350 |
| 1 | 1E | 335 |
| 1 | 1F | 275 |
| 2 | 2A | 200 |
| 2 | 2B | 300 |
| 2 | 2C | 400 |
| 2 | 2D | 250 |
| 2 | 2E | 350 |
+--------+---------+-------+
虽然我知道如何根据一个变量创建调色板,但我想根据每个变量创建一个调色板"parent"
所以假设我想要 1 = "Reds"
和 2 = "Blues"
但是在这两种 "parent" 颜色中,我希望这些区域内的所有地形都根据销售情况进行着色?
这没有明确使用 leaflet
,而是使用 RColorBrewer
(传单导入)
此函数将按给定的第一列拆分数据框,并依次为第二列中的每个值赋予每个 Pals 的颜色。
最后我将其分配给了一个列,但您可能希望以不同的方式使用它。
library(RColorBrewer)
library(dplyr)
createCustomPalette <- function(df, parentCol = "region",subCol = "terrain",
Pals = c("Blues", "Reds", "Greens", "Greys", "Oranges", "Purples"),
returnDF = FALSE){
Parents <- as.numeric(unique(df[[parentCol]]))
ParCols <- lapply(seq_along(Parents), function(x)
RColorBrewer::brewer.pal(length(unique(df[df[parentCol]==Parents[x],subCol])), Pals[x]))
Cols <- unlist(ParCols)
Colsdf <- data.frame(Col = Cols, sub = unlist(lapply(Parents, function(x) unique(df[df[[parentCol]]==x,][[subCol]]))),
stringsAsFactors = F)
outdf <- dplyr::right_join(Colsdf,df, by = c("sub"=subCol))
# outdf <- merge(df, Colsdf, by.x = subCol, by.y = "sub", all.x = T, sort = FALSE )
if(returnDF){
return(outdf)
}else{
return(outdf$Col)
}
}
mydf$Cols <- createCustomPalette(mydf)
scales::show_col(mydf$Cols)
我尽量避免使用 dplyr
,因为我不想添加新的库,但是合并和维护行顺序(如果您只是获取颜色,这很重要)在 base R 中是一个痛苦。如果你不想使用 dplyr
你只需要添加行号并合并然后排序。
您可以使用library(colourvalues)
为向量/数据列生成特定颜色
设置数据
## using a known / reproducible data object
df <- mapdeck::capitals
## making a 'region' column to represent your data
df$region <- ifelse( tolower( substr(df$country,1,1) ) %in% letters[1:13], 1, 2 )
## a random 'sales' column
df$sales <- rnorm(n = nrow(df) )
着色
library(colourvalues)
## order the data first so we can simply `<- c()` the result
df <- df[with(df, order(region)), ]
region1 <- colour_values( x = df[ df$region == 1, "sales"], palette = "reds" )
region2 <- colour_values( x = df[ df$region == 2, "sales"], palette = "blues")
df$colours <- c(region1, region2)
绘图
library(leaflet)
leaflet() %>%
addTiles() %>%
addCircles(
data = df
, color = df$colours
)