Dennis
Dennis

Reputation: 33

echarts4r: fix axis range across groups

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)?

Upvotes: 2

Views: 823

Answers (1)

stefan
stefan

Reputation: 124013

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

Upvotes: 2

Related Questions