2

I am creating a grouped bar chart like so:

library(tidyverse)
library(echarts4r)
data("starwars")

starwars %>% 
  group_by(sex, eye_color) %>% 
  summarise(height = mean(height, na.rm=TRUE)) %>% 
  group_by(sex) %>% 
  e_charts(x = eye_color, timeline = TRUE) %>%
  e_bar(height, legend = FALSE)

enter image description here

How do I set the range of the y axis (height) to be the same across groups (sex)?

Phil
  • 7,287
  • 3
  • 36
  • 66
Dennis
  • 23
  • 5

1 Answers1

1

You could set maximum value for the y axis using e_y_axis(max = XXX), e.g. in the code below I set the max value based on the maximum of height.

library(tidyverse)
library(echarts4r)
data("starwars")

ymax <- max()
dat <- starwars %>% 
  group_by(sex, eye_color) %>% 
  summarise(height = mean(height, na.rm=TRUE), .groups = "drop") 

ymax <- 50 * ceiling(max(dat$height, na.rm = TRUE) / 50)

dat %>% 
  group_by(sex) %>% 
  e_charts(x = eye_color, timeline = TRUE) %>%
  e_bar(height, legend = FALSE) %>%
  e_y_axis(max = ymax)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51