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

Stacked bar plot & e_connect with e_river #597

Open
MrMisc opened this issue Nov 29, 2023 · 7 comments
Open

Stacked bar plot & e_connect with e_river #597

MrMisc opened this issue Nov 29, 2023 · 7 comments
Labels

Comments

@MrMisc
Copy link

MrMisc commented Nov 29, 2023

I have another question regarding creating stacked barplots. These always seem a little difficult to do. Consider the following,
DirectExample.csv

##If you want to import from here
data<-read.csv("DirectExample.csv")

data|>group_by(Country)|>e_charts(Year)|>
  e_bar(n)|>
  e_legend(right = 5,top = 80,selector = "inverse",show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0))), align = 'right',type = "scroll",width = 10,orient = "vertical")

For my case, the plot ends up with a very large range for Year which I do not know the cause of. Additonally, I want this plot to be a stacked barplot where the x-axis would be Year and the y-axis would be count. I was hoping for the colours of each of the countries be represented as distinct "stacks" per Year represented by a bar.

I haven't been having much luck looking online for this type of stuff.

I am wondering if anyone has a solution to this such that I can also link it appropriately with a current working river plot that is as follows

#River Plot
data|>group_by(Country)|>e_charts(Year)|>
  e_river(n)|>
  e_legend(right = 5,top = 30,bottom = 100,selector = "inverse",show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0))), align = 'right',type = "scroll",width = 10,orient = "vertical")|>
  e_theme("westeros")|>e_tooltip(trigger = "axis")|>
  e_datazoom(
    type = "slider",
    toolbox = TRUE,
    bottom = 10
  )|>
  e_title("ALL Anthrax Incident Reports by Year", "WAHIS Public Quantitative data from WHO")

I was hoping to use e_connect or e_brush to pair the 2 charts together nicely.
However, when I try to do something like that, I just get a blank screen like for the following attempt.

#Attempt at combining both plots
data|>group_by(Country)|>e_charts(Year)|>
  e_river(n)|>
  e_legend(right = 5,top = 30,bottom = 100,selector = "inverse",show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0))), align = 'right',type = "scroll",width = 10,orient = "vertical")|>
  e_theme("westeros")|>e_tooltip(trigger = "axis")|>
  e_bar(n, stack = "group")|>
  e_x_axis(min = min(data$Year)-1,max = max(data$Year)+1)|>
  e_grid(right = 40, top = 100, width = "30%")

Clearly I am using it wrongly, but I haven't quite figured out what the cause might be.

Thank you for your time!

@JohnCoene
Copy link
Owner

Thank you for the detailed post but could you give a minimal reproducible example that does not require download a CSV?

@MrMisc
Copy link
Author

MrMisc commented Nov 29, 2023

Sure. I think this example might work equivalently for now

data<-data.frame(
  Country = rep(c("Afghanistan", "Albania", "Angola", "Argentina"), each = 5),
  Year = rep(c(2010, 2019, 2017, 2009, 2014), 4),
  n = sample(1:100, 20, replace = TRUE),
  Deaths = sample(1:1500, 20, replace = TRUE),
  Cases = sample(1:2000, 20, replace = TRUE),
  lethality = runif(20, min = 0, max = 1),
  database_name.x = rep(c("Islamic Republic of Afghanistan", "Republic of Albania", "Republic of Angola", "Argentine Republic"), each = 5),
  lat.x = rep(c(33.00, 41.00, -12.50, -34.00), each = 5)
)

@JohnCoene
Copy link
Owner

Year is numeric here so it's going to be treated as such and start from 0

data |> dplyr::mutate(Year = as.factor(Year)) |> group_by(Country) |> e_chart(Year) |> e_bar(n, stack = "group")

@MrMisc
Copy link
Author

MrMisc commented Nov 29, 2023

Interesting. The problem now is that the order of the years is not respected.

@rdatasculptor
Copy link
Contributor

You can define the order of the factor levels using factor()

@MrMisc
Copy link
Author

MrMisc commented Nov 29, 2023

Why wouldn't something like say,

data|>group_by(Country)|>e_charts(Year)|>
  e_bar(n, stack = "group")|>
  e_legend(right = 5,top = 80,selector = "inverse",emphasis = list(selectorLabel = list(offset = list(10,0)), focus = "series"),show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0))), align = 'right',type = "scroll",width = 10,orient = "vertical")|>
  e_x_axis(min = min(data$Year)-1,max = max(data$Year)+1)|>
  e_tooltip()

Not work?

It looks like it almost would, however, the stack isn't perfect. Many countries disappear behind others in this configuration for some reason

@oobd
Copy link

oobd commented Dec 5, 2023

It seems to work fine...
Or is it breaking down when more countries are added?

library(forcats)
library(dplyr)

data <- data %>% arrange(Year) %>% mutate(Year = fct_inorder(as.character(Year), ordered = T))

data|>group_by(Country)|>e_charts(Year)|>
e_bar(n, stack = "group")|>
e_legend(right = 5,top = 80,selector = "inverse",emphasis = list(selectorLabel = list(offset = list(10,0)), focus = "series"),show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0))), align = 'right',type = "scroll",width = 10,orient = "vertical")|>
e_x_axis(min = min(data$Year)-1,max = max(data$Year)+1)|>
e_tooltip()

image

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

No branches or pull requests

5 participants