R Highcharter问题,在使用hc_yAxis_multiples的y轴上添加plotBands

问题描述 投票:1回答:1

试图添加带有y轴LHS的plotBand,但出现错误

错误:所有参数都必须命名为列表

参考数据集中的示例:

library(highcharter)
library(dplyr)

df1 <- data.frame(month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                  values1 = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2,
                              26.5, 23.3, 18.3, 13.9, 9.6), stringsAsFactors = F)

df1$values2 <- df1$values1/5

图表:

hc <- highchart() %>% 
        hc_yAxis_multiples(
          list(lineWidth = 3),
          list(showLastLabel = T, opposite = TRUE)) %>% 
        hc_title(text = "A nice chart") %>% 
        hc_chart(type = "column") %>% 
        hc_xAxis(categories = df1$month) %>% 
        hc_add_series(data = df1$values1) %>%
        hc_add_series(data = df1$values2, type = "spline", color = "#1FA67A", yAxis = 1)

hc <- hc %>% 
        hc_tooltip(crosshairs = TRUE, shared = TRUE) %>% 
        hc_yAxis(minorGridLineWidth = 0, gridLineWidth = 0,
                 plotBands = list(
                   list(from = 10, to = 20, color = "rgba(68, 170, 213, 0.1)",
                        label = list(text = "A low band")),
                   list(from = 20, to = 25, color = "rgba(0, 0, 0, 0.1)",
                        label = list(text = "A medium band")),
                   list(from = 25, to = 30, color = "rgba(68, 170, 213, 0.1)",
                        label = list(text = "A high band"))
                 ))

r highcharts
1个回答
0
投票

由于使用了hc_yAxis_multiples,因此应在此列表中指定每个轴的配置。此后再次调用hc_yAxis时,它不知道要放置plotBand等哪个轴。]

尝试这样的事情:

hc <- highchart() %>% 
        hc_yAxis_multiples(
          list(lineWidth = 3,
               minorGridLineWidth = 0, 
               gridLineWidth = 0,
                 plotBands = list(
                   list(from = 10, to = 20, color = "rgba(68, 170, 213, 0.1)",
                        label = list(text = "A low band")),
                   list(from = 20, to = 25, color = "rgba(0, 0, 0, 0.1)",
                        label = list(text = "A medium band")),
                   list(from = 25, to = 30, color = "rgba(68, 170, 213, 0.1)",
                        label = list(text = "A high band"))
                 )
          ),
          list(minorGridLineWidth = 0,gridLineWidth = 0,
          showLastLabel = T, opposite = TRUE)) %>% 
        hc_title(text = "A nice chart") %>% 
        hc_chart(type = "column") %>% 
        hc_xAxis(categories = df1$month) %>% 
        hc_add_series(data = df1$values1) %>%
        hc_add_series(data = df1$values2, type = "spline", color = "#1FA67A", yAxis = 1)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.