From https://github.com/jbkunst/highcharter/issues/153
library(highcharter)
library(dplyr)
library(tidyr)
options(highcharter.theme = hc_theme_smpl())
txt <- "Month | Impressions | Clicks | CTR | CPC | Position | Cost | Conversions | Conv.rate | Cost_conv
2016-07-01 | 1385 | 161 | 11.6 | 0.2 | 1.7 | 36.3 | 3.33 | 2.1 | 10.9"
con <- textConnection(txt)
df <- read.table(con, sep = "|", header = TRUE)
df <- df[,-1]
df
## Impressions Clicks CTR CPC Position Cost Conversions Conv.rate
## 1 1385 161 11.6 0.2 1.7 36.3 3.33 2.1
## Cost_conv
## 1 10.9
# TRY 1 -------------------------------------------------------------------
df2 <- gather(df, name, y)
df2
## name y
## 1 Impressions 1385.00
## 2 Clicks 161.00
## 3 CTR 11.60
## 4 CPC 0.20
## 5 Position 1.70
## 6 Cost 36.30
## 7 Conversions 3.33
## 8 Conv.rate 2.10
## 9 Cost_conv 10.90
df2 <- df2 %>%
mutate(x = row_number() - 1) %>%
group_by(name) %>%
do(data = list((list(x = .$x, y = .$y)))) %>%
mutate(yAxis = ifelse(name %in% c("Impressions", "Clicks"), 1, 0))
df2
## Source: local data frame [9 x 3]
## Groups: <by row>
##
## # A tibble: 9 x 3
## name data yAxis
## <chr> <list> <dbl>
## 1 Clicks <list [1]> 1
## 2 Conv.rate <list [1]> 0
## 3 Conversions <list [1]> 0
## 4 Cost <list [1]> 0
## 5 Cost_conv <list [1]> 0
## 6 CPC <list [1]> 0
## 7 CTR <list [1]> 0
## 8 Impressions <list [1]> 1
## 9 Position <list [1]> 0
series <- list_parse(df2)
series[[1]]
## $name
## [1] "Clicks"
##
## $data
## $data[[1]]
## $data[[1]]$x
## [1] 1
##
## $data[[1]]$y
## [1] 161
##
##
##
## $yAxis
## [1] 1
hc <- highchart() %>%
hc_xAxis(type = "category", categories = df2$name) %>%
hc_yAxis_multiples (
list(
title = list(text = "%, posição e custos"),
align = "right",
opposite = TRUE
),
list(
title = list(text = "Impressões, cliques e outros"),
align = "left"
)
) %>%
hc_add_series_list(series)
hc
hc_chart(hc, type = "column")
# TRY 2 -------------------------------------------------------------------
df3 <- df %>%
gather(name, y) %>%
mutate(x = row_number() - 1,
group = ifelse(name %in% c("Impressions", "Clicks"), 1, 0)) %>%
group_by(group) %>%
do(data = list_parse(data_frame(x = .$x, y = .$y, name = .$name))) %>%
mutate(yAxis = group)
df3
## Source: local data frame [2 x 3]
## Groups: <by row>
##
## # A tibble: 2 x 3
## group data yAxis
## <dbl> <list> <dbl>
## 1 0 <list [7]> 0
## 2 1 <list [2]> 1
series <- list_parse(df3)
series[[2]]
## $group
## [1] 1
##
## $data
## $data[[1]]
## $data[[1]]$x
## [1] 0
##
## $data[[1]]$y
## [1] 1385
##
## $data[[1]]$name
## [1] "Impressions"
##
##
## $data[[2]]
## $data[[2]]$x
## [1] 1
##
## $data[[2]]$y
## [1] 161
##
## $data[[2]]$name
## [1] "Clicks"
##
##
##
## $yAxis
## [1] 1
hc2 <- highchart() %>%
hc_xAxis(categories = names(df)) %>%
hc_yAxis_multiples (
list(
title = list(text = "%, posição e custos"),
align = "right",
opposite = TRUE
),
list(
title = list(text = "Impressões, cliques e outros"),
align = "left"
)
) %>%
hc_add_series_list(series)
hc2
hc_chart(hc2, type = "column")
this work a little if you deselect some series