Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X-axis max doubled when adding stacked e_bars? #357

Closed
sharlagelfand opened this issue Aug 5, 2021 · 3 comments
Closed

X-axis max doubled when adding stacked e_bars? #357

sharlagelfand opened this issue Aug 5, 2021 · 3 comments

Comments

@sharlagelfand
Copy link

Hi! Thanks for this great package :)

I'm running into a strange issue where the x-axis max is doubled (?) when I'm adding two e_bar()s that are stacked. I want to create a bar chart where one bar is "highlighted", so I'm accomplishing that with two series.

The x limits in the data are the same, and they each appear separately just fine:

library(echarts4r)
library(dplyr)

df_1 <- data.frame(
  x = 1:5, 
  y_1 = c(1, 2, 3, NA, 5)
)

df_1 %>%
  e_chart(x) %>%
  e_bar(y_1)

Screen Shot 2021-08-05 at 2 17 32 PM

df_2 <- data.frame(
  x = 1:5, 
  y_2 = c(NA, NA, NA, 4, NA)
)

df_2 %>%
  e_chart(x) %>%
  e_bar(y_2)

Screen Shot 2021-08-05 at 2 17 51 PM

When I combine, the styling looks as desired - I'm "fake" stacking them I guess, because I don't want the dodged behaviour, and that looks fine - but the x-axis max is doubled, going all the way to 10 instead of 5:

df_combined <- df_1 %>%
  left_join(df_2, by = "x")

df_combined %>%
  e_chart(x) %>%
  e_bar(y_1, stack = "group") %>%
  e_bar(y_2, stack = "group")

Screen Shot 2021-08-05 at 2 18 46 PM

Any ideas why this might be? I'm using the latest version from github. I know you can manually set the limits, but it's hard to do that on a dynamic / not-manual basis. Thank you!

@JohnCoene
Copy link
Owner

If that's of any consolation, it was difficult for me to figure out too.

In Echarts.js the bar serie is "optimised" for a categorical x axis, it never really works right with a number type on the x axis.
Internally, echarts4r infers the type it sets in Echarts.js based on the data it is being fed.
Converting x to factor or categorical corrects this.

df_combined %>%
  mutate(x = as.factor(x)) %>% 
  e_chart(x) %>%
  e_bar(y_1, stack = "group") %>%
  e_bar(y_2, stack = "group")

With the above echarts4r observes that the data on the x axis is factor and correctly communicates to Echarts.js that
the x axis should be categorical.

Let me know if this is not clear.
I should probably makes this clearer in the documentation.

@JohnCoene
Copy link
Owner

I've added a note about it in the documentation.

I should perhaps force the data type to categorical internally but I'm worried this may impact other things. I'll think about it.

Thanks again for reporting this.

@sharlagelfand
Copy link
Author

Ahhh that makes sense, thanks for clarifying! Looks great now.

Also helps me understand why manually setting the limits didn't really do what I want - was definitely acting factor-like! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants