rechartsecharts4r

echarts4r - stacked bar with label for individual segments and total value


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")

stacked bar with labeled segments

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.


Solution

  • 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 the name attribute of the data and can be added via the formatter "{b}". Finally, thanks to the comment by @Jan we can set legend=NULL to get rid of the additional series in the legend.

    library(echarts4r)
    
    dataset <- data.frame(
      categories = letters[1:5],
      smaller = 11:15,
      larger = 21:25,
      total = 11:15 + 21:25
    )
    dataset$zero <- 0
    
    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") |>
      e_bar(
        serie = zero, bind = total, name = "Total",
        stack = "stack",
        label = list(
          show = TRUE,
          formatter = "{b}",
          position = "top"
        ),
        legend = NULL
      )
    

    enter image description here