library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
hSeries2 <- function(df, series) {
  # TO DO !!!! Check input that series is a character and is in the given data.frame
  seriesList <- by(df, as.factor(df[,series]), function(df.s) {
    # remove the column series of the data.frame
    seriesName <- as.character(df.s[1,series])
    df.s <- df.s[,-which(colnames(df.s) == series)]
    list(data = list_parse(df.s), name = seriesName)
  }, simplify = F)
  attributes(seriesList) <- NULL
  
  seriesList
}

library(swiTheme)

x <- 1:10
y <- seq(1, 100, 10)
z <- 10:1
color <- as.factor(rep(c("grey", "red"), 5))
name <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
series <- c(rep(c("blob", "poop", "doop"), 3), "asdf")

df <- data.frame(x = x, y = y, z = z, color = color, name = name, series = series)
head(df)
##   x  y  z color name series
## 1 1  1 10  grey    a   blob
## 2 2 11  9   red    b   poop
## 3 3 21  8  grey    c   doop
## 4 4 31  7   red    d   blob
## 5 5 41  6  grey    e   poop
## 6 6 51  5   red    f   doop
hSeries2 <- hSeries2(df, "series")
# hSeries1 <- hSeries(x,y,z,name, color, series)

str(hSeries2, max.level = 4)
## List of 4
##  $ :List of 2
##   ..$ data:List of 1
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 10
##   .. .. ..$ y    : num 91
##   .. .. ..$ z    : int 1
##   .. .. ..$ color: chr "red"
##   .. .. ..$ name : chr "j"
##   ..$ name: chr "asdf"
##  $ :List of 2
##   ..$ data:List of 3
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 1
##   .. .. ..$ y    : num 1
##   .. .. ..$ z    : int 10
##   .. .. ..$ color: chr "grey"
##   .. .. ..$ name : chr "a"
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 4
##   .. .. ..$ y    : num 31
##   .. .. ..$ z    : int 7
##   .. .. ..$ color: chr "red"
##   .. .. ..$ name : chr "d"
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 7
##   .. .. ..$ y    : num 61
##   .. .. ..$ z    : int 4
##   .. .. ..$ color: chr "grey"
##   .. .. ..$ name : chr "g"
##   ..$ name: chr "blob"
##  $ :List of 2
##   ..$ data:List of 3
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 3
##   .. .. ..$ y    : num 21
##   .. .. ..$ z    : int 8
##   .. .. ..$ color: chr "grey"
##   .. .. ..$ name : chr "c"
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 6
##   .. .. ..$ y    : num 51
##   .. .. ..$ z    : int 5
##   .. .. ..$ color: chr "red"
##   .. .. ..$ name : chr "f"
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 9
##   .. .. ..$ y    : num 81
##   .. .. ..$ z    : int 2
##   .. .. ..$ color: chr "grey"
##   .. .. ..$ name : chr "i"
##   ..$ name: chr "doop"
##  $ :List of 2
##   ..$ data:List of 3
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 2
##   .. .. ..$ y    : num 11
##   .. .. ..$ z    : int 9
##   .. .. ..$ color: chr "red"
##   .. .. ..$ name : chr "b"
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 5
##   .. .. ..$ y    : num 41
##   .. .. ..$ z    : int 6
##   .. .. ..$ color: chr "grey"
##   .. .. ..$ name : chr "e"
##   .. ..$ :List of 5
##   .. .. ..$ x    : int 8
##   .. .. ..$ y    : num 71
##   .. .. ..$ z    : int 3
##   .. .. ..$ color: chr "red"
##   .. .. ..$ name : chr "h"
##   ..$ name: chr "poop"
hSeries2[[1]]
## $data
## $data[[1]]
## $data[[1]]$x
## [1] 10
## 
## $data[[1]]$y
## [1] 91
## 
## $data[[1]]$z
## [1] 1
## 
## $data[[1]]$color
## [1] "red"
## 
## $data[[1]]$name
## [1] "j"
## 
## 
## 
## $name
## [1] "asdf"
head(df)
##   x  y  z color name series
## 1 1  1 10  grey    a   blob
## 2 2 11  9   red    b   poop
## 3 3 21  8  grey    c   doop
## 4 4 31  7   red    d   blob
## 5 5 41  6  grey    e   poop
## 6 6 51  5   red    f   doop
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.3.2
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
# we can group by a variable (the series) and 
# create the  (list) 
hcseries <- df %>%
  group_by(series) %>% 
  do(data = list_parse(select(., -series))) %>% 
  rename(name = series)

hcseries
## Source: local data frame [4 x 2]
## Groups: <by row>
## 
## # A tibble: 4 × 2
##     name       data
## * <fctr>     <list>
## 1   asdf <list [1]>
## 2   blob <list [3]>
## 3   doop <list [3]>
## 4   poop <list [3]>
# every row is a series which you can add highcharts options, for example:
set.seed(123)
hcseries$showInLegend = c(TRUE, FALSE)

# or add the legend color in case the points dont have colors
hcseries$color <- c("red", "blue", "green", "black")

hcseries
## Source: local data frame [4 x 4]
## Groups: <by row>
## 
## # A tibble: 4 × 4
##     name       data showInLegend color
## * <fctr>     <list>        <lgl> <chr>
## 1   asdf <list [1]>         TRUE   red
## 2   blob <list [3]>        FALSE  blue
## 3   doop <list [3]>         TRUE green
## 4   poop <list [3]>        FALSE black
# here we do a list of series
hcserieslist <- list_parse(hcseries)

hcserieslist[[1]]
## $name
## [1] "asdf"
## 
## $data
## $data[[1]]
## $data[[1]]$x
## [1] 10
## 
## $data[[1]]$y
## [1] 91
## 
## $data[[1]]$z
## [1] 1
## 
## $data[[1]]$color
## [1] "red"
## 
## $data[[1]]$name
## [1] "j"
## 
## 
## 
## $showInLegend
## [1] TRUE
## 
## $color
## [1] "red"
highchart() %>% 
  hc_chart(type = "bubble") %>% 
  hc_add_series_list(hcserieslist)
highchart() %>% 
  hc_chart(type = "bubble") %>% 
  hc_add_series_list(hSeries2)