R 中的 Highcharts 为具有相同 y 值的 xrange 类型图的每个系列启用不同的颜色?

问题描述 投票:0回答:2

我想为“xrange”类型的图的不同系列使用不同的颜色。当同一行上有多个系列(相同的 y 值)时,这不起作用。一些帮助将不胜感激。

这是示例代码块。我希望所有三个系列都有不同的颜色。

# Create a data frame with three series
df <- data.frame(start = c(-10, -20, -30), end = c(10, 20, 30), y = rep(1, 3))

# Create a color vector with three colors
colors <- c("#FF0000", "#00FF00", "#0000FF")

highchart() %>%
  hc_plotOptions(xrange = list(grouping = FALSE, colorByPoint = TRUE), 
                 enableMouseTracking = TRUE, 
                 column = list(dataLabels = list(enabled = TRUE))) %>%
  hc_add_series(df[1, ], "xrange", color = colors[1], hcaes(x = start, x2 = end, y = y), name = "Series 1") %>%
  hc_add_series(df[2, ], "xrange", color = colors[2], hcaes(x = start, x2 = end, y = y), name = "Series 2") %>%
  hc_add_series(df[3, ], "xrange", color = colors[3], hcaes(x = start, x2 = end, y = y), name = "Series 3") %>%
  hc_xAxis(min = -100, max = 100, title = FALSE, plotLines = list(list(value = 0)))

理想情况下我会拥有这样的东西。

enter image description here

javascript r highcharts
2个回答
0
投票

要在 Highcharts R 的“xrange”类型图中为不同的系列设置不同的颜色,您可以在 hc_add_series() 函数中为每个系列设置颜色参数。在此示例中,每个系列在 y 轴上移动一个单位,因此它们不在同一行上。每个系列的颜色参数设置为不同的颜色。让我知道它是否对您有帮助!

library(highcharter)

# Create a data frame with three series
df <- data.frame(start = c(-10, -20, -30), end = c(10, 20, 30), y = rep(1, 3))

# Create a color vector with three colors
colors <- c("#FF0000", "#00FF00", "#0000FF")

highchart() %>%
  hc_plotOptions(xrange = list(grouping = FALSE, colorByPoint = TRUE), 
                 enableMouseTracking = TRUE, 
                 column = list(dataLabels = list(enabled = TRUE))) %>%
  hc_add_series(df[1, ], "xrange", color = colors[1], hcaes(x = start, x2 = end, y = y), name = "Series 1") %>%
  hc_add_series(df[2, ], "xrange", color = colors[2], hcaes(x = start, x2 = end, y = y + 1), name = "Series 2") %>%
  hc_add_series(df[3, ], "xrange", color = colors[3], hcaes(x = start, x2 = end, y = y + 2), name = "Series 3") %>%
  hc_xAxis(min = -100, max = 100, title = FALSE, plotLines = list(list(value = 0)))

0
投票

你的意思是这样的吗?

# Create a data frame with three series
df <- data.frame(start = c(-10, -0, 20), end = c(0, 20, 30), y = rep(1, 3))

# Create a color vector with three colors
colors <- c("#FF0000", "#00FF00", "#0000FF")

highchart() %>%
  hc_plotOptions(xrange = list(grouping = FALSE, colorByPoint = FALSE), 
                 enableMouseTracking = TRUE, 
                 column = list(dataLabels = list(enabled = TRUE))) %>%
  hc_add_series(df[1, ], "xrange", color = colors[1], hcaes(x = start, x2 = end, y = y), name = "Series 1") %>%
  hc_add_series(df[2, ], "xrange", color = colors[2], hcaes(x = start, x2 = end, y = y), name = "Series 2") %>%
  hc_add_series(df[3, ], "xrange", color = colors[3], hcaes(x = start, x2 = end, y = y), name = "Series 3") %>%
  hc_xAxis(min = -100, max = 100, title = FALSE, plotLines = list(list(value = 0)))
© www.soinside.com 2019 - 2024. All rights reserved.