Highcharts plot multiple series without multiple hc_add_series

highcharter
#1

Hello everyone,

Most of the time I user highcharter library to plot specific amount of time-series this way:

highchart() %>% 
hc_add_series(name = "DF Column name", data = df$VALUE, color = ... ) %>%
hc_add_series(name = "DF Column name2", data = df$VALUE2, color = ... )

Sometimes however I don't know the amount of columns and their names in data frame - e.g. when the data frame is a result of dcast() function.
I would like the library to plot all of the columns, set series name as column names and assign colors from a default color-order pallette.
It is easily done using dygraphs library where I decide whether I want to specify the series myself or I want the library to do this for me:

toPlotTestData <- xts(testDataResult[,2:4], order.by = testDataResult[, 1], tzone = "GMT")
dygraph(toPlotTestData, main = "Title") 

vs

toPlotTestData <- xts(testDataResult[,2:4], order.by = testDataResult[, 1], tzone = "GMT")
dygraph(toPlotTestData, main = "Title") %>% 
dySeries("COL1", label="COL1", axis = 'y', color = "blue") %>%
dySeries("COL2", label="COL2", axis = 'y', color = "orange")

Is it also possible in highcharter?
Any help appreciated.

0 Likes

#2

You can refer to the link
https://dantonnoriega.github.io/ultinomics.org/post/2017-04-05-highcharter-explainer.html

There are many ways.

  1. hcaes()
  2. hchart() with tidy data.
2 Likes

#3

Thank you for the link! It is a very good article showing how to use Highcharts documentation to create charts in R/Shiny.

It appears that while dygraphs library needs data in "wide" format, highcharter needs melted data ("long" format) to create dynamic number of plots.
Below is the solution I started to use after reading the article:

  • using reshape2::melt function as opposed to tidyr::gather suggested in the article
  • with datetime x axis as opposed to categories
require(reshape2)

output$plot <- renderHighchart({
      dataMelted <- melt(data, id=c("DAY"), value.name="VALUES", variable.name="PARAMS")
      dataMelted$DAY <- datetime_to_timestamp(dataMelted$DAY)
      hchart(dataMelted, type = 'line', hcaes(y = VALUES, group = PARAMS, x = DAY)) %>% 
        hc_xAxis(type = "datetime") 
})
0 Likes

#4

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

0 Likes

closed #5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes