I'm trying to make a stacked bar chart in echarts4r that would label both individual segments' values, as well as the total. Here is a short example:
library(echarts4r)
dataset <- data.frame(
categories = letters[1:5],
smaller = 11:15,
larger = 21:25,
total = 11:15 + 21:25
)
e_charts(dataset, x = categories) %>%
e_bar(serie = larger, name = "Big", stack = "stack") %>%
e_bar(serie = smaller, name = "Small", stack = "stack") %>%
e_labels(position = "inside")
With this, I'm able to get the values of segments labelled as is the goal. However, I can't figure out how to add a label at the end of the bars that would also label the "total" column.
A possible R approach building on the idea of @MatthiasMertens. As in his approach I add a third empty or zero series for the totals but pass the total values via the
bind=
argument. This way the totals get stored in thename
attribute of the data and can be added via the formatter"{b}"
. Finally, thanks to the comment by @Jan we can setlegend=NULL
to get rid of the additional series in the legend.