highcharts 在R Highchart中,如何用不同的颜色对折线图的不同部分进行着色

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

这是我的代码和highchart图表:

library(tidyverse)
library(gapminder)
library(highchart)

highchart() %>%
  hc_add_series(
    data = gapminder %>% filter(country == 'Chile'),
    hcaes(x = year, y = pop),
    color = 'red',
    type = 'line'
  )

我想着色blue的部分,我的图表有关的年above 1990
输出应该是一个图表,红色线从1952年到1990年,蓝色线从1990年到2005年。

只有一个hc_add_series函数才是可靠

有什么帮助吗?

n6lpvg4x

n6lpvg4x1#

编辑

如果您只想使用一个hc_add_series,您可以将zoneszoneAxis一起使用。在zone中,您可以根据zoneAxis、x或y的范围设置所需的valuecolor。在您的情况下,它是x,表示1990年。下面您可以使用一个可重复的示例:

library(tidyverse)
library(gapminder)
library(highcharter)

# > Registered S3 method overwritten by 'quantmod':

# >   method            from

# >   as.zoo.data.frame zoo

highchart() %>%
  hc_add_series(
    data = gapminder %>% filter(country == 'Chile'),
    hcaes(x = year, y = pop),
    type = 'line',
    zoneAxis = 'x',
    zones = list(list(value = 1990, color = 'red'), list(value = 1990, color = 'blue'))
  )

创建于2022年10月7日,使用reprex v2.0.2
你可以用两个filter创建两个hc_add_series。唯一的问题可能是没有1990年的数据,所以我给了你一个可能的选择,如下所示:

library(tidyverse)
library(gapminder)
library(highcharter)

# > Registered S3 method overwritten by 'quantmod':

# >   method            from

# >   as.zoo.data.frame zoo

# > Highcharts (www.highcharts.com) is a Highsoft software product which is

# > not free for commercial and Governmental use

highchart() %>%
  hc_add_series(
    data = gapminder %>% filter(country == 'Chile' & year <= 1990),
    hcaes(x = year, y = pop),
    color = 'red',
    type = 'line'
  ) %>%
  hc_add_series(
    data = gapminder %>% filter(country == 'Chile' & year > 1990),
    hcaes(x = year, y = pop),
    color = 'blue',
    type = 'line'
  )

highchart() %>%
  hc_add_series(
    data = gapminder %>% filter(country == 'Chile' & year <= 1992),
    hcaes(x = year, y = pop),
    color = 'red',
    type = 'line'
  ) %>%
  hc_add_series(
    data = gapminder %>% filter(country == 'Chile' & year > 1991),
    hcaes(x = year, y = pop),
    color = 'blue',
    type = 'line'
  )

创建于2022年10月2日,使用reprex v2.0.2

lqfhib0f

lqfhib0f2#

为实现系列的不同颜色,可以设置区域,给予为每个值范围设置颜色。

Highcharts.chart('container', {
    series: [{
        data: [-10, -5, 0, 5, 10, 15, 10, 10, 5, 0, -5],
        zones: [{
            value: 0,
            color: '#f7a35c'
        }, {
            value: 10,
            color: '#7cb5ec'
        }, {
            color: '#90ed7d'
        }]
    }]
});

https://api.highcharts.com/highstock/plotOptions.series.zones

相关问题