Другим подходом может быть использование highcharter
library(dplyr)
library(tidyr)
library(highcharter)
#convert your data in wide format
df <- df %>% spread(metric, value)
#plot
highchart() %>%
hc_xAxis(categories = df$categories) %>%
hc_yAxis_multiples(
list(lineWidth = 3, title = list(text = "Page")),
list(opposite = TRUE, title = list(text = "Product"))
) %>%
hc_add_series(type = "column", data = df$page) %>%
hc_add_series(type = "line", data = df$product, yAxis=1) # replace "line" with "column" to have it in bar format
Выходной участок:
Пример данных:
df <- structure(list(categories = structure(c(4L, 3L, 2L, 1L, 4L, 3L,
2L, 1L), .Label = c("cultural events", "economy", "local", "politics"
), class = "factor"), metric = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L), .Label = c("page", "product"), class = "factor"),
value = c(100L, 50L, 20L, 19L, 950000L, 470000L, 50000L,
1320L)), .Names = c("categories", "metric", "value"), row.names = c(NA,
-8L), class = "data.frame")