Highcharter 中的自定义颜色未正确映射到图例
Custom colors in Highcharter are not mapping correctly to the legend
我很难将特定颜色映射到堆叠条形图的值,以便它们在从同一数据集构建的大量图表中保持一致。例如,highcharter 会将默认颜色(或我给它的颜色列表)分配给我的分组分类值,但是当图表被修改时(比如在 Shiny 中),会生成一个新图表,但颜色可能已经改变。这对我的听众来说非常混乱,并且可能会误导他们。
这是一个示例数据集:
responses <- c('Pro','Against','Neutral','Resigned/Accepting','Not Specified')
constituents <- c('dual degree','law only','undergrad only','friend','parent only')
indiv <- rep(1:50)
Name.Change <- sample(responses,50,replace = TRUE)
constituent.type <- sample(constituents,50,replace = TRUE)
demo <- as.data.frame(cbind(indiv,Name.Change,constituent.type))
我对堆叠变量进行因式分解,因为这似乎始终如一地确定了堆叠的顺序。然后我将颜色分配给 'Name.Change' 的值,以便它们在各种图表中保持一致。
demo$Name.Change.fac <- factor(demo$Name.Change, levels = c("Pro","Resigned/Accepting","Neutral","Against","Not Specified"), ordered = TRUE)
demo <- demo %>%
mutate(
name.change.color = plyr::mapvalues(
Name.Change.fac,
from = c(
"Pro",
"Against",
"Resigned/Accepting",
"Neutral",
"Not Specified"
),
to = c("#1395BA","#F16C20","#0D3C55","#EBC844","#A2B86C")
)
)
这是我对 HighCharter 的最佳尝试:
demo %>% ## these colors don't match
group_by(constituent.type,Name.Change.fac,name.change.color) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change.fac,
color = name.change.color),
color = unique(demo$name.change.color)) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
如您所见,标签中的颜色与堆叠图表中(正确分配的)颜色不对应。如果我在 hcaes()
中删除 color=name.change.color
,条形图和标签之间的颜色匹配,但颜色不是我分配的颜色,并且可能会因图表而异。我探索了手动重新创建图例的方法,但我无法使用反应式点击功能来切换图表中的可见条形。
这是我的图书馆:
library('plyr')
library('dplyr')
library('tidyr')
library('highcharter')
尝试
color = levels(demo$name.change.color)
而不是
color = unique(demo$name.change.color)
给你正确的顺序。
如果您在代码中的某处使用唯一或下降因子水平,事情可能会变得不正常。
或者,您可以像这样简化事情:
invisible(suppressPackageStartupMessages(lapply(c("dplyr","tidyr","highcharter"),
require, character.only=TRUE)))
responses <- c('Pro','Against','Neutral','Resigned/Accepting','Not Specified')
constituents <- c('dual degree','law only','undergrad only','friend','parent only')
indiv <- rep(1:50)
set.seed(123)
Name.Change <- factor(sample(responses, 50, replace = TRUE),
levels = responses, ordered = TRUE)
constituent.type <- sample(constituents, 50, replace = TRUE)
demo <- tibble(indiv, Name.Change, constituent.type)
name.change.color <- setNames(
c("#1395BA","#F16C20","#0D3C55","#EBC844","#A2B86C"),
levels(demo$Name.Change))
demo %>%
group_by(constituent.type, Name.Change) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change),
color = name.change.color
) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
#> `summarise()` regrouping output by 'constituent.type' (override with `.groups` argument)
由 reprex package (v0.3.0)
于 2020-07-31 创建
我很难将特定颜色映射到堆叠条形图的值,以便它们在从同一数据集构建的大量图表中保持一致。例如,highcharter 会将默认颜色(或我给它的颜色列表)分配给我的分组分类值,但是当图表被修改时(比如在 Shiny 中),会生成一个新图表,但颜色可能已经改变。这对我的听众来说非常混乱,并且可能会误导他们。
这是一个示例数据集:
responses <- c('Pro','Against','Neutral','Resigned/Accepting','Not Specified')
constituents <- c('dual degree','law only','undergrad only','friend','parent only')
indiv <- rep(1:50)
Name.Change <- sample(responses,50,replace = TRUE)
constituent.type <- sample(constituents,50,replace = TRUE)
demo <- as.data.frame(cbind(indiv,Name.Change,constituent.type))
我对堆叠变量进行因式分解,因为这似乎始终如一地确定了堆叠的顺序。然后我将颜色分配给 'Name.Change' 的值,以便它们在各种图表中保持一致。
demo$Name.Change.fac <- factor(demo$Name.Change, levels = c("Pro","Resigned/Accepting","Neutral","Against","Not Specified"), ordered = TRUE)
demo <- demo %>%
mutate(
name.change.color = plyr::mapvalues(
Name.Change.fac,
from = c(
"Pro",
"Against",
"Resigned/Accepting",
"Neutral",
"Not Specified"
),
to = c("#1395BA","#F16C20","#0D3C55","#EBC844","#A2B86C")
)
)
这是我对 HighCharter 的最佳尝试:
demo %>% ## these colors don't match
group_by(constituent.type,Name.Change.fac,name.change.color) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change.fac,
color = name.change.color),
color = unique(demo$name.change.color)) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
如您所见,标签中的颜色与堆叠图表中(正确分配的)颜色不对应。如果我在 hcaes()
中删除 color=name.change.color
,条形图和标签之间的颜色匹配,但颜色不是我分配的颜色,并且可能会因图表而异。我探索了手动重新创建图例的方法,但我无法使用反应式点击功能来切换图表中的可见条形。
这是我的图书馆:
library('plyr')
library('dplyr')
library('tidyr')
library('highcharter')
尝试
color = levels(demo$name.change.color)
而不是
color = unique(demo$name.change.color)
给你正确的顺序。
如果您在代码中的某处使用唯一或下降因子水平,事情可能会变得不正常。
或者,您可以像这样简化事情:
invisible(suppressPackageStartupMessages(lapply(c("dplyr","tidyr","highcharter"),
require, character.only=TRUE)))
responses <- c('Pro','Against','Neutral','Resigned/Accepting','Not Specified')
constituents <- c('dual degree','law only','undergrad only','friend','parent only')
indiv <- rep(1:50)
set.seed(123)
Name.Change <- factor(sample(responses, 50, replace = TRUE),
levels = responses, ordered = TRUE)
constituent.type <- sample(constituents, 50, replace = TRUE)
demo <- tibble(indiv, Name.Change, constituent.type)
name.change.color <- setNames(
c("#1395BA","#F16C20","#0D3C55","#EBC844","#A2B86C"),
levels(demo$Name.Change))
demo %>%
group_by(constituent.type, Name.Change) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change),
color = name.change.color
) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
#> `summarise()` regrouping output by 'constituent.type' (override with `.groups` argument)
由 reprex package (v0.3.0)
于 2020-07-31 创建