在highcharter中为hc_add_series_list定义x值

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

我想为高级图线图定义x值,因为我有分类x值,有时也有引用相同类别的值。无论如何在高图上,有可能在12月15日的这个例子中显示:https://whatukthinks.org/eu/questions/should-the-united-kingdom-remain-a-member-of-the-eu-or-leave-the-eu/?removed。使用函数hc_add_series,我可以定义x值,如下例所示:

library(highcharter)
df = data_frame(x = c(1,1,3,3,5:7),y = c(100,105,110,120,80,90,98),name = 
c("A","A","A","A","B","B","B"))

hc2 <- highchart() %>%
    hc_add_series(df)

hc2

在我的情况下,我不知道在开头会有多少系列,所以我想使用hc_add_series_list,因为我可以预先定义列表。但后来我无法定义x值,因为数据总是被解释为y值。在我阅读https://dantonnoriega.github.io/ultinomics.org/post/2017-04-05-highcharter-explainer.html后,我认为应该可以使用子列表,但直到现在我还没有真正成功。我这样试过:

ds_new <- lapply((1:1), function(x){
  list(name = paste(trips_of_choice[x]),
       data = list(x = c(1:2),
                   y = c(1:2),
                   name =c("5.TA.26-3-j19-1.5.R")
       ))
})

因此,要有一个列表,其中包含名为data的子列表,并包含x和y数据。但它根本不起作用。

我希望有人可以帮助我

r list highcharts r-highcharter
1个回答
1
投票

假设您的系列存储在列表中:

df1 <- data.frame(x = c(1,2,3,4),
                  y = c(100,105,110,120),
                  name = c("A","A","A","A"))
df2 <- data.frame(x = c(3:6),
                  y = c(80,90,98,105),
                  name = c("B","B","B","B"))
series_list <- list(df1, df2)

您可以使用此解决方案绘制所有系列:

hc2 <- highchart()
for (k in 1:length(series_list)) {
   hc2 <- hc2 %>%
          hc_add_series(series_list[[k]])
}
hc2

enter image description here

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