library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
options(highcharter.theme = hc_theme_smpl())
data("iris")
iris <- tbl_df(iris)
glimpse(iris)
## Observations: 150
## Variables: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9,...
## $ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1,...
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5,...
## $ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1,...
## $ Species      <fctr> setosa, setosa, setosa, setosa, setosa, setosa, ...

version 1

# version 1 ---------------------------------------------------------------
highchart() %>% 
  hc_add_series(iris, "scatter", hcaes(x = Petal.Length, y = Sepal.Length),
                name = "Sepal.Length", yAxis = 0) %>% 
  hc_add_series(iris, "scatter", hcaes(x = Petal.Length, y = Sepal.Width),
                name = "Sepal.Width", yAxis = 1) %>% 
  hc_yAxis_multiples(
   list(title = list(text = "Sepal.Length")),
   list(title = list(text = "Sepal.Width"), opposite = TRUE)
  )

version 2

# version2 ----------------------------------------------------------------
library(tidyr)

iris2 <- iris %>% 
  select(Petal.Length, Sepal.Length, Sepal.Width) %>% 
  gather(Sepal, value, -Petal.Length) 
  

iris2
## # A tibble: 300 × 3
##    Petal.Length        Sepal value
##           <dbl>        <chr> <dbl>
## 1           1.4 Sepal.Length   5.1
## 2           1.4 Sepal.Length   4.9
## 3           1.3 Sepal.Length   4.7
## 4           1.5 Sepal.Length   4.6
## 5           1.4 Sepal.Length   5.0
## 6           1.7 Sepal.Length   5.4
## 7           1.4 Sepal.Length   4.6
## 8           1.5 Sepal.Length   5.0
## 9           1.4 Sepal.Length   4.4
## 10          1.5 Sepal.Length   4.9
## # ... with 290 more rows
hchart(iris2, "scatter", hcaes(x = Petal.Length, y = value, group = Sepal),
       yAxis = c(0, 1))  %>% 
  hc_yAxis_multiples(
    list(title = list(text = "Sepal.Length")),
    list(title = list(text = "Sepal.Width"), opposite = TRUE)
  )

version 3

# version 3 ---------------------------------------------------------------

seriesiris <- iris2 %>% 
  group_by(name = Sepal) %>% 
  do(data = list_parse2(data.frame(.$Petal.Length, .$value))) %>% 
  ungroup() %>% 
  mutate(yAxis = c(0, 1))

seriesiris
## # A tibble: 2 × 3
##           name         data yAxis
##          <chr>       <list> <dbl>
## 1 Sepal.Length <list [150]>     0
## 2  Sepal.Width <list [150]>     1
highchart() %>% 
  hc_chart(type = "scatter") %>% 
  hc_add_series_list(seriesiris)  %>% 
  hc_yAxis_multiples(
    list(title = list(text = "Sepal.Length")),
    list(title = list(text = "Sepal.Width"), opposite = TRUE)
  )