JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
585 stars 82 forks source link

multiple label positions for charts using group_by #580

Closed oobd closed 8 months ago

oobd commented 8 months ago

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

library(echarts4r)
library(tidyverse)

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)

d |>
  group_by(groups) |>
  e_chart(xaxis) |>
  e_bar(value, stack = "groups",
     bind = Label,
     label = list(
       show = TRUE,
       formatter = "{b}",
       position = "inside"
     )
  )

Result is this where both labels are position 'inside': image

i can specify multiple positions if i create a stacked chart using multiple e_bar like below

individual bar charts

df <- data.frame(
  x = LETTERS[1:10],
  a = runif(10),
  b = runif(10),
  c = runif(10),
  d = runif(10)
)

df <- df %>% mutate(round_a = round(a),
                    round_d = round(d) + 100)

df %>% 
  e_charts(x) %>% 
  e_bar(a, stack = "grp", 
        bind = round_a,
     label = list(
       show = TRUE,
       formatter = "{b}",
       position = "inside"
     )) %>% 
  e_bar(b, stack = "grp") %>% 
  e_bar(c, stack = "grp2") %>% 
  e_bar(d, stack = "grp2",
        bind = round_d,
     label = list(
       show = TRUE,
       formatter = "{b}",
       position = "top"
     ))

Result is this where one of the label is position 'inside' and other is 'top' image

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?

rdatasculptor commented 8 months ago

I think you can use e_add_nested("label", position) for this purpose.

oobd commented 8 months ago

Ah great, thanks, it worked 😄

reprex and output below

image

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)