jyjek
jyjek

Reputation: 2707

stacked axis in echarts4r R


How to make stacked plot in echarts4r package?
If I add second y-axis - it's add new series in same plot.

data.frame(x=LETTERS[1:5],y=1:5,
               z=6:10)%>%
  e_charts(x)%>%
  e_line(y)%>%
  e_line(z, y.index = 1)

enter image description here

But I'm need some like that:
enter image description here
Thanks!

Upvotes: 2

Views: 676

Answers (1)

JohnCoene
JohnCoene

Reputation: 2261

As it is you plot on two different Y axis (y.index = 1) so you will no be able to stack them, if you plot them on the same Y axis you will be able to stack them.

data.frame(x=LETTERS[1:5],
           y=1:5,
           z=6:10
) %>%
  e_charts(x) %>%
  e_line(y, stack = "stack") %>%
  e_line(z, stack = "stack")

Note that you do not have to pass "stack" to the stack argument, you can pass whatever you want, this enables different groups to be stacked.

data.frame(x = LETTERS[1:5],
           y=1:5,
           z = 6:10,
           w = rnorm(5, 4, 1),
           e = rnorm(5, 5, 2)
) %>%
  e_charts(x) %>%
  e_bar(y, stack = "stack") %>%
  e_bar(z, stack = "stack") %>%
  e_bar(w , stack = "grp2") %>%
  e_bar(e, stack = "grp2")

Reason why this option is not explicitly stated in documentation: ECharts comes with hundreds if not thousands of options, listing them all as arguments would be neigh impossible. All options are, however, available in the package; see the official documention

EDIT

You can stack on multiple Y axis but not across them. This works too:

data.frame(x = LETTERS[1:5],
           y=1:5,
           z = 6:10,
           w = rnorm(5, 4, 1),
           e = rnorm(5, 5, 2)
) %>%
  e_charts(x) %>%
  e_bar(y, stack = "stack") %>% # defaults to y.index = 0
  e_bar(z, stack = "stack") %>% # defaults to y.index = 0
  e_bar(w , stack = "grp2", y.index = 1) %>% # secondary axis + stack
  e_bar(e, stack = "grp2", y.index = 1) # secondary axis + stack

For multiple charts as you initially wanted, one x axis, multiple Y axis, 2 plots on one page:

df <- data.frame(x=LETTERS[1:5],y=1:5, z=6:10)

df %>% 
  e_charts(x) %>% 
  e_line(y) %>% 
  e_line(z, y.index = 1, x.index = 1) %>% 
  e_y_axis(gridIndex = 1) %>%
  e_x_axis(gridIndex = 1) %>% 
  e_grid(height = "35%") %>% 
  e_grid(height = "35%", top = "50%") %>% 
  e_datazoom(x.index = c(0, 1)) # brush http://echarts4r.john-coene.com/articles/brush.html

Upvotes: 1

Related Questions