0

I am a bit confused with use of highcharter hc_add_series function.

I am trying to create a plot where I need to specify both x and y axis, where x axis are continuous. I have a data-frame, for example:

df_plot <- cbind(
  seq(0, 1, by = 0.1),
  sample(seq(from = 100, to = 300, by = 10), size = 11, replace = TRUE),
  sample(seq(from = 1, to = 100, by = 9), size = 11, replace = TRUE),
  sample(seq(from = 50, to = 60, by = 2), size = 11, replace = TRUE),
  sample(seq(from = 100, to = 130, by = 1), size = 1, replace = TRUE)
) %>% 
  as.data.frame()

names(df_plot) <- c("x", "a", "b", "c", "d")

I saw this example that works

highchart() %>%
    hc_add_series(data = purrr::map(4:8, function(x) list(x, x)), color = "blue")

So i tried:

df_plot1 <- Map(cbind, split.default(df_plot[-1], names(df_plot)[-1]), x=df_plot[1])

highchart() %>%
  hc_add_series(data = df_plot1[[1]]) %>%
  hc_add_series(data = df_plot1[[2]], yAxis = 1) %>%
 hc_yAxis_multiples(
    list(lineWidth = 3, lineColor='#7cb5ec', title=list(text="First y-axis")),
    list(lineWidth = 3, lineColor="#434348", title=list(text="Second y-axis")))

However, I am getting "No data to display" on the plot, so I obviously went wrong somewhere.

Also, I cannot use hchart function, as I need have multiple y axis

2 Answers2

2

After reading docs about split.default it Divide into Groups and Reassemble, however you need to access the variable you want to plot, e.g. df_plot1[[1]$a, like so:

library(highcharter)
df_plot <- cbind(
  seq(0, 1, by = 0.1),
  sample(seq(from = 100, to = 300, by = 10), size = 11, replace = TRUE),
  sample(seq(from = 1, to = 100, by = 9), size = 11, replace = TRUE),
  sample(seq(from = 50, to = 60, by = 2), size = 11, replace = TRUE),
  sample(seq(from = 100, to = 130, by = 1), size = 1, replace = TRUE)
) %>% as.data.frame()

names(df_plot) <- c("x", "a", "b", "c", "d")
df_plot1 <- Map(cbind, split.default(df_plot[-1], names(df_plot)[-1]), x=df_plot[1])

highchart() %>%
  hc_xAxis(categories = df_plot1[[1]]$x) %>%
  hc_add_series(data = df_plot1[[1]]$a) %>%
  hc_add_series(data = df_plot1[[2]]$b, yAxis = 1) %>%
  hc_yAxis_multiples(
    list(lineWidth = 3, lineColor='#7cb5ec', title=list(text="First y-axis")),
    list(lineWidth = 3, lineColor="#434348", title=list(text="Second y-axis")))

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • but this doesn't change x axis, they are just numbers from 0 to 10, while it should be from 0 to 1. This is just exactly the same if I didn't convert it to list by simply said `hc_add_series(df_plot$a)` – Galina Polishchuk Aug 29 '17 at 07:41
  • Ah sorry comrade, I didnt know what the problem was, I have updated the answer – Pork Chop Aug 29 '17 at 08:11
  • This is great, thanks. The only issue is that now highchart is treating my x axis as a category, is there a way to keep in numeric? E.g. if I have as my x axis `seq(0, 24, by = 0.1)` and on the plot I would like to have only integer ticks (I found `allowDecimals = FALSE` for this, but it doesn't work when my x axis is a characer) but when i hover over the line, still see original 0.1, 0.2 and so on. – Galina Polishchuk Aug 29 '17 at 09:55
  • I can do `mapply(function(x, y) list(x, y), x=df_plot$x, y=df_plot$a, SIMPLIFY = FALSE)` instead of `Map()` – Galina Polishchuk Aug 31 '17 at 07:43
  • I'm happy you found the solution – Pork Chop Aug 31 '17 at 08:15
1

not sure if this can help you,

library(tidyr)
df_plot2 <- gather(df_plot, group, y, -x)

hchart(df_plot2, "line", hcaes(x, y, group = group)) 

hchart(df_plot2, "line", hcaes(x, y, group = group), yAxis = 0:3) %>% 
  hc_yAxis_multiples(
    list(lineWidth = 3, title=list(text="First y-axis")),
    list(lineWidth = 3, title=list(text="Second y-axis")),
    list(lineWidth = 3, title=list(text="3rd y-axis")),
    list(lineWidth = 3, title=list(text="4th y-axis"))
    )
jbkunst
  • 3,009
  • 1
  • 22
  • 28