echarts4r
echarts4r copied to clipboard
Changing result based on legend selection
Hi all!
I am using Echarts4r to make a Shiny app. My data frame is wide, and the idea is to show the cumulative sum for the stacked bar chart. I created the variable for cumulative sum called "cum_sum_total" for the 3 programs, but when I un-click some category on the legend, I would like to have a new cumulative sum according with the categories that I choose to keep.
Please, any advice would be really appreciate,
Many thanks! D.
Here the code:
data2 <- data.frame(zipcode= as.factor(round(rnorm(10, 87965, 50),0)), program_1 = rnorm(10, 200, 20), program_2 = rnorm(10, 700, 100), program_3 = rnorm(10, 500, 50))
data<- data2 %>%
mutate(total = rowSums(across(where(is.numeric)))) %>%
mutate(per_program_1=round(100*program_1/total,1)) %>%
mutate(per_program_2=round(100*program_2/total,1)) %>%
mutate(per_program_3=round(100*program_3/total,1)) %>%
mutate("per_total"=round(100*total/sum(total),1)) %>%
arrange(desc(total)) %>%
mutate(cum_sum_total = round(cumsum(per_total),1)) %>%
mutate(per_total_label = paste0(per_total |> as.character(), "%")) |>
mutate(cum_sum_label = paste0(cum_sum_total |> as.character(), "%")) %>%
mutate(zipcode = fct_reorder(zipcode, desc(total)))
data |>
e_charts(zipcode) |>
e_bar(program_1, stack = "g", bind=per_program_1, name="A", legend=TRUE, y_index = 0, emphasis = list(focus = "series")) |>
e_bar(program_2, stack = "g", bind=per_program_2, name="B", legend=TRUE, y_index = 0, emphasis = list(focus = "series")) |>
e_bar(program_3, stack = "g", bind=per_program_3, name="C", legend=TRUE, y_index = 0, emphasis = list(focus = "series")) |>
e_line(legend=T, cum_sum_total, bind=cum_sum_label, y_index = 1, name = "Cumulative %", emphasis = list(focus = "series"),
tooltip=list(show=F),
label = list(
show = TRUE, formatter = "{b}",
rotate = 0,
textStyle = list(fontFamily = "Arial",
fontSize = 10))) |>
e_x_axis(name = "Zip Code", show=TRUE,
nameLocation = "middle", nameGap=50,
nameTextStyle = list(
color = "#87786B",
fontSize = 15),
axisLabel = list(fontFamily="Arial",
interval = 0, rotate = 30,
fontSize=12, color="#87786B")) |>
e_y_axis(name="n° of visits", index = 0, show = TRUE,
nameLocation ="middle",
nameGap=70, align="right",
nameTextStyle = list(
color = "#87786B",
fontSize = 12),
axisLabel = list(fontSize=12, color="#87786B")) |>
e_legend(itemWidth=30, top = 30, right=10,
type="scroll", padding=0,
selected = list('A' = TRUE,
'B' = TRUE,
'C' = TRUE,
'Cumulative %' = FALSE),
textStyle=list(fontFamily="arial", fontSize=12,
color="#87786B")) %>%
e_tooltip(#trigger = "item",
formatter = htmlwidgets::JS("function(params){return(params.marker + params.seriesName +
'<br>' +
'Total: ' + echarts.format.addCommas(params.value[1]) + ' (' +
params.name + '%)');}"),
textStyle=list(fontFamily="Arial", fontSize=12)) |>
e_animation(duration=1500) |>
e_toolbox(show =T, top=0, right= 10, itemSize=10, emphasis=list(iconStyle=list(textFill="#6C2C4E"))) |>
e_toolbox_feature(feature = c("saveAsImage", "restore")) |>
e_theme_custom('{"color":["#F8AD3B", "#D15425", "#9CAC3B", "#6C2C4E"]}')