将 Highcharter 条形图中分组变量的某些值设置为默认取消选择
Set certain values of grouped variable in Highcharter bar plot to be deselected by default
我希望在堆叠式 Highcharter 条形图中我的分组变量的某些值在默认情况下被取消选择。我知道使用 hc_add_series
我可以设置 visible = FALSE
但我不会以这种方式生成我的图表。
这是一个示例数据集:
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))
这是图表
demo %>%
group_by(constituent.type,Name.Change) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change)) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
生成的图表选择了 Name.Change
的所有值,并且可以根据需要取消选择每个值。但是,我希望默认情况下取消选择某些值(例如“中性”和“未指定”),这样在呈现图表后,您必须单击图例中的该值才能显示在图表上.
我认为最好用chart.events.load来做,然后你可以为此编写自己的自定义JS函数:
https://api.highcharts.com/highcharts/chart.events.load
在这里你可以看到如何做到这一点:
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))
demo %>%
group_by(constituent.type,Name.Change) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change)) %>%
hc_chart(events = list(load = JS("function() {
var chart = this;
chart.series[1].setVisible(false)
chart.series[2].setVisible(false)
}"))) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
我希望在堆叠式 Highcharter 条形图中我的分组变量的某些值在默认情况下被取消选择。我知道使用 hc_add_series
我可以设置 visible = FALSE
但我不会以这种方式生成我的图表。
这是一个示例数据集:
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))
这是图表
demo %>%
group_by(constituent.type,Name.Change) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change)) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
生成的图表选择了 Name.Change
的所有值,并且可以根据需要取消选择每个值。但是,我希望默认情况下取消选择某些值(例如“中性”和“未指定”),这样在呈现图表后,您必须单击图例中的该值才能显示在图表上.
我认为最好用chart.events.load来做,然后你可以为此编写自己的自定义JS函数: https://api.highcharts.com/highcharts/chart.events.load
在这里你可以看到如何做到这一点:
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))
demo %>%
group_by(constituent.type,Name.Change) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = count,
x = constituent.type,
group = Name.Change)) %>%
hc_chart(events = list(load = JS("function() {
var chart = this;
chart.series[1].setVisible(false)
chart.series[2].setVisible(false)
}"))) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)