简体   繁体   English

设置Highcharter栏中分组变量的某些值plot默认取消选择

[英]Set certain values of grouped variable in Highcharter bar plot to be deselected by default

I would like certain values of my grouped variable in a stacked Highcharter barplot to be deselected by default.我希望默认取消选择堆叠的 Highcharter 条形图中的分组变量的某些值。 I know that with hc_add_series I can set visible = FALSE but I am not generating my chart in that way.我知道使用hc_add_series我可以设置visible = FALSE但我没有以这种方式生成我的图表。

Here is a sample data set:这是一个示例数据集:

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))

And here is the chart这是图表

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)

The chart generated has all values of Name.Change selected and each value can be deselected as desired.生成的图表选择了Name.Change的所有值,并且可以根据需要取消选择每个值。 However, I want certain values (eg "Neutral" and "Not Specified") to be deselected by default, such that after the chart is rendered, you would have to click that value in the legend in order for it to appear on the chart.但是,我希望默认取消选择某些值(例如“中性”和“未指定”),以便在呈现图表后,您必须单击图例中的该值才能使其显示在图表上.

I think it's best to do it with chart.events.load, then you can write your own custom JS function for this: https://api.highcharts.com/highcharts/chart.events.load我认为最好用chart.events.load来做,然后你可以为此编写自己的自定义JS function: https://api.Z676C9A4107F4D29F41F958B661000

Here you can see how to do it:在这里你可以看到如何做到这一点:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM