highcharts 在Highchart R包中如何显示多个折线图,但仅从2条线开始

bfhwhh0e  于 2022-11-10  发布在  Highcharts
关注(0)|答案(2)|浏览(275)

我想绘制一个5个国家的 highcharts 。
但是我想从“智利”和“阿根廷”两条线开始绘制图表,然后我会让用户通过点击其他国家的图例来交互地添加其他国家。
这可能吗?

library(highcharter)
library(gapminder)

gapminder %>%  filter(country == c("Chile","Argentina","Peru","Brazil","Portugal")) %>%
  hchart("line",
        hcaes(x = year, y = pop, group = country))
zpjtge22

zpjtge221#

一种方法是分两步加载国家/地区,第一步标记为visible = FALSE
不幸的是,这会使组不按字母顺序排列,因此需要对更多的层进行一些手动操作,以控制排序和初始可见性。

gap_vis <- subset(gapminder, country %in% c("Chile","Argentina"))
gap_hid <- subset(gapminder, country %in% c("Peru","Brazil","Portugal"))

hchart(gap_vis, "line", hcaes(x = year, y = pop, group = country)) %>%
hc_add_series(gap_hid, "line", hcaes(x = year, y = pop, group = country), 
              visible = FALSE)

hfsqlsce

hfsqlsce2#

你只需要为正确的序列设置visible: false

series: [{
    name: 'Chile',
    data: [...]
  }, {
    name: 'Argentina',
    data: [...]
  }, {
    name: 'Peru',
    data: [...],
    visible: false
  }, {
    name: 'Brazil',
    data: [...],
    visible: false
  }, {
    name: 'Portugal',
    data: [...],
    visible: false
  }]

现场演示:http://jsfiddle.net/BlackLabel/4ebnj0Lq/
API引用:https://api.highcharts.com/highcharts/series.line.visible

相关问题