How to create a motion line chart wth highcharter in R

I am trying to create a motion line diagram. The functionality will look something like this:

library(highcharter)
library(magrittr)

highchart() %>% 
  hc_chart(type = "line") %>% 
  hc_yAxis(max = 12, min = 0) %>%
  hc_xAxis(categories = c(1, 1.7, 1, 0)) %>% 
  hc_add_series(data = list(
              list(sequence = c(1,1,1,1)),
              list(sequence = c(NA,2,2,2)),
              list(sequence = c(NA,NA,5,5)),
              list(sequence = c(NA,NA,NA,10))
            )) %>% 
  hc_motion(enabled = TRUE, labels = 1:4, series = 0)

      

timetable

But I would like the end result to look like this using the option hc_motion

hchart(data.frame(xx=c(1, 1.7, 1, 0), yy=c(1, 2, 5, 10)), 
       type = "line", hcaes(x = xx, y = yy))

      

spline

ie the problem is in the first case the motion diagram is treating xAxis

as categories whereas I would like it to be like a scatter plot with straight lines.

+3


source to share


1 answer


After some searching I have an answer

library(highcharter)
library(magrittr)

      

Data creation

df <- data.frame(xx=c(1, 1.7, 1, 0), yy=c(1, 2, 5, 10))
df$key <- 0
df <- lapply(1:dim(df)[1], function(x){ df$key <- df$key+x; df}) %>% 
do.call(rbind, .)
df %<>% group_by(key) %>% mutate(key2=1, key2=cumsum(key2)) %>% ungroup %>% 
  mutate(yy=ifelse(key<key2, NA, yy))

      

Some manipulations



To enter a form highcharter

withhc_motion

df1 <- df %>% filter(key==1) %>% select(-key)
df2 <- df %>% group_by(key2) %>% do(sequence = list_parse(select(., y = yy))) %>% ungroup
df <- left_join(df1, df2, by="key2")
df %<>% select(xx, yy, sequence)

      

count

highchart() %>% hc_yAxis(min = 0, max = 10) %>%
  hc_add_series(data = df, type = "line", hcaes(x = xx, y = yy)) %>%
  hc_motion(enabled = TRUE, series = 0, startIndex = 0, labels = 1:4)

      

solution

+1


source







All Articles