I have a stacked bar chart and I want to add a value label above each stacked bar. I don't want values for each section of the stack.
This yields a value for each section of the stack:
library(echarts4r)
set.seed(1)
d <- data.frame(
xaxis = c(rep("a", 2), rep("b", 2)),
groups = c("c", "d", "c", "d"),
value = rnorm(4, mean = 50)
)
d |>
group_by(groups) |>
e_chart(xaxis) |>
e_bar(value, stack = "grp1") |>
e_labels()
I just want one number above each bar, equal to the sum of each section.
You can precalculate the labels of your groups and then bind them to e_bar :
library(echarts4r)
set.seed(1)
d <- data.frame(
xaxis = c(rep("a", 2), rep("b", 2)),
groups = c("c", "d", "c", "d"),
value = rnorm(4, mean = 50)
) |>
group_by(xaxis) |>
dplyr::mutate(Label = ifelse(groups == "c","",as.character(sum(value))))
d |>
group_by(groups) |>
e_chart(xaxis) |>
e_bar(value, stack = "groups",
bind = Label,
label = list(
show = TRUE,
formatter = "{b}",
position = "outside"
)
)