Closed oobd closed 8 months ago
I think you can use e_add_nested("label", position)
for this purpose.
Ah great, thanks, it worked 😄
reprex and output below
library(echarts4r)
library(tidyverse)
# create sample dataset with 3 groups to stack (c, d, totals)
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 = value) %>%
ungroup()
test <- d %>%
mutate(groups = 'test') %>%
distinct(xaxis, .keep_all = T) %>%
mutate(value = value + 20) %>%
mutate(Label = value)
combined <- rbind(d, test)
# here the variable name has to match the argument used in e_add_nested (in this case, position)
combined <- combined %>%
mutate(position = case_when(groups == 'c' ~ 'inside',
groups == 'test' ~ 'left',
T ~ 'right'))
combined |>
group_by(groups) |>
e_chart(xaxis) |>
e_bar(value, stack = "groups",
bind = Label,
label = list(
show = TRUE,
formatter = "{b}"
)) %>%
e_add_nested("label", position)
is there a way to specify multiple label positions for charts that use group_by?
for example, i can get a stacked bar chart using group_by like below but this only let's me specify one label position for both top and bottom bars (both use 'inside')
stacked bar using group_by
Result is this where both labels are position 'inside':![image](https://github.com/JohnCoene/echarts4r/assets/115593238/bc25d0ff-3be0-443f-8be3-fb3852ed4cc3)
i can specify multiple positions if i create a stacked chart using multiple e_bar like below
individual bar charts
Result is this where one of the label is position 'inside' and other is 'top'![image](https://github.com/JohnCoene/echarts4r/assets/115593238/8c31437e-331c-446d-8d21-7ef0320c7565)
but this has the issue of requiring e_bar to be repeated for all groups that i want to stack. is there a way to use the first method of using group_by and specifying different label positions as well?