bullet chart in echarts4r

28 Views Asked by At

I want to create bullet charts with {echarts4r}. What I have is this:

library(echarts4r)
library(dplyr)

mtcars[1:5,] |>
  tibble::rownames_to_column("model") |>
  mutate(total = mpg + qsec) |>
  arrange(desc(total)) |>
  e_charts(model) |>
  e_bar(mpg, stack = "grp", barWidth = "90%", barCategoryGab = "20%") |>
  e_bar(qsec, stack = "grp") |> 
  e_bar(gear, stack = "grp2", barWidth = "45%") |> 
  e_flip_coords()

How can I put the third e_bar() in front of the other stack?

Any help appreciated.

enter image description here

1

There are 1 best solutions below

0
eastclintw00d On

I found an example solution here

and adapted it to {echarts4r}

library(echarts4r)

df1 <- 
  data.frame(
    type = c("A", "B", "C", "D", "E"),
    achieved = c(110, 94, 97, 78, 68),
    target = c(95, 90, 90, 90, 100),
    pass = c(70, 70, 70, 70, 70),
    good = c(15, 15, 15, 15, 15),
    excellent = c(15, 15, 15, 15, 15)
  )

df1 %>% 
  e_charts(type) %>% 
  e_bar(achieved, name = "Completion Rate", barGap = "-300%", barWidth = 10, z = 10) %>% 
  e_scatter(target, name = "Planned Completion Rate", symbol = "rect", silent = TRUE, symbolSize = c(30, 5), symbolOffset = c(0, -10), symbolRotate = 90, z = 20, color = "#000") %>% 
  e_bar(pass, name = "Pass", barWidth = 50, stack = "total") %>% 
  e_bar(good, name = "Good", barWidth = 50, stack = "total") %>% 
  e_bar(excellent, name = "Excellent", barWidth = 50, stack = "total") %>% 
  e_flip_coords() %>% 
  e_legend(icon = "roundRect")

enter image description here