Один из способов сделать это, очень похоже на highcharter
:
dt %>%
e_charts(a) %>%
e_area(x, stack = "stack", origin = 'start') %>%
e_area(y, stack = "stack", origin = 'start') %>%
e_area(z, stack = "stack", origin = 'start') %>%
e_grid(left = '4%', right = '3%', bottom = '10%', containLabel = TRUE) %>%
e_toolbox(left = 'right', itemSize = 15, top = 22) %>%
e_toolbox_feature("saveAsImage", title = 'save as image') %>%
e_toolbox_feature("dataZoom", title = list(zoom = "zoom", back = "back")) %>%
e_toolbox_feature("restore", title = 'restore') %>%
e_theme("infographic") %>%
e_legend(type = 'scroll', bottom = 1) %>%
e_tooltip( trigger ="axis",
formatter = htmlwidgets::JS("
function(params){
var tot = params[0].name + params[1].value[0] + params[2].value[0]
function perc(x){return(Math.round((x/tot) * 100))};
return('x: ' + params[0].value[1] + ' (' + perc(params[0].value[1]) + '%)' + '<br>' + 'y:' + params[1].value[1] + ' (' + perc(params[1].value[1]) + '%)' + '<br>' + 'z:' + params[2].value[1] + ' (' + perc(params[2].value[1]) + '%)')
}
")
)
Больше информации о подсказках на сайте .
РЕДАКТИРОВАТЬ Спасибо за ваш комментарий, извините, я пропустил часть вопроса.По какой-то причине, о которой я не знал, стекирование работает только с категориальной осью х.Ниже приведены два варианта:
# options one use character or vector.
dt %>%
dplyr::mutate(a = as.character(a)) %>%
e_charts(a) %>%
e_area(x, stack = "stack") %>%
e_area(y, stack = "stack") %>%
e_area(z, stack = "stack") %>%
e_grid(left = '4%', right = '3%', bottom = '10%') %>%
e_toolbox(left = 'right', itemSize = 15, top = 22) %>%
e_toolbox_feature("saveAsImage", title = 'save as image') %>%
e_toolbox_feature("restore", title = 'restore') %>%
e_theme("infographic") %>%
e_legend(bottom = 1) %>%
e_tooltip( trigger ="axis",
formatter = htmlwidgets::JS("
function(params){
var tot = params[0].name + params[1].value[0] + params[2].value[0]
function perc(x){return(Math.round((x/tot) * 100))};
return('x: ' + params[0].value[1] + ' (' + perc(params[0].value[1]) + '%)' + '<br>' + 'y:' + params[1].value[1] + ' (' + perc(params[1].value[1]) + '%)' + '<br>' + 'z:' + params[2].value[1] + ' (' + perc(params[2].value[1]) + '%)')
}
")
)
# option 2
# Use e_x_axis to change the axis type
dt %>%
e_charts(a) %>%
e_area(x, stack = "stack") %>%
e_area(y, stack = "stack") %>%
e_area(z, stack = "stack") %>%
e_grid(left = '4%', right = '3%', bottom = '10%') %>%
e_toolbox(left = 'right', itemSize = 15, top = 22) %>%
e_toolbox_feature("saveAsImage", title = 'save as image') %>%
e_toolbox_feature("restore", title = 'restore') %>%
e_theme("infographic") %>%
e_legend(bottom = 1) %>%
e_tooltip( trigger ="axis",
formatter = htmlwidgets::JS("
function(params){
var tot = params[0].name + params[1].value[0] + params[2].value[0]
function perc(x){return(Math.round((x/tot) * 100))};
return('x: ' + params[0].value[1] + ' (' + perc(params[0].value[1]) + '%)' + '<br>' + 'y:' + params[1].value[1] + ' (' + perc(params[1].value[1]) + '%)' + '<br>' + 'z:' + params[2].value[1] + ' (' + perc(params[2].value[1]) + '%)')
}
")
) %>%
e_x_axis(type = "category")