Short version: Is there a way to plot bar charts with side-by-side bars (something like position=dodge option in ggplot) using echarts4r?
Long version: I want to create a bar chart grouped by multiple variables and side-by-side bars using echarts4r. For example, with the following data:
data <- tibble(
Year = c(rep(c(rep("2021", 2), rep("2022", 2)), 3)),
Month = c(rep("3", 4), rep("10", 4), rep("12", 4)),
Vehicle = rep(c("car", "bus", "tram"), 4),
N = runif(12, min=30, max=100)
)
I want to have Vehicles on the x-axis and N on the y-axis, but for each vehicle I want to group the data by Month and Year and plot the corresponding N as a separate bar.
I managed to do that in the following way:
data <- data |> unite(Period, Month, Year, sep = "/") |> group_by(Period)
data %>%
pivot_wider(names_from = Period, values_from = N) %>%
e_charts(Vehicle) %>%
e_bar(3/2021) %>%
e_bar(10/2021) %>%
e_bar(12/2021) %>%
e_bar(3/2022) %>%
e_bar(10/2022) %>%
e_bar(12/2022)
This code produces the following chart: Desired look of my bar chart This is exactly how I want my chart to look, however the problem is that this is a part of an interactive shiny app and the user should be able to select months and years that they want, so neither the values in my Period column nor their number is constant. Hence, I can't explicitly plot each individual e_bar(month/year).
I am looking for a solution that would be as straightforward as
data <- data |> unite(Period, Month, Year, sep = "/") |> group_by(Period)
data |> e_charts(Vehicle, stack = "grp") |> e_bar(N)
but instead of a stacked bar chart, it should have side-by-side bars.
Not sure what's exactly the issue. Drop grp="stacked"
and you will get a grouped barchart. And the only thing to fix is getting the right order which can be achieved by converting Period
to a factor
for which I use interaction
instead of unite()
:
data <- tibble::tibble(
Year = c(rep(c(rep("2021", 2), rep("2022", 2)), 3)),
Month = c(rep("3", 4), rep("10", 4), rep("12", 4)),
Vehicle = rep(c("car", "bus", "tram"), 4),
N = runif(12, min = 30, max = 100)
)
library(tidyverse)
library(echarts4r)
data |>
mutate(
Period = interaction(
as.integer(Month), as.integer(Year),
sep = "/"
)
) |>
group_by(Period) |>
e_charts(Vehicle) |>
e_bar(N)