JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
585 stars 82 forks source link

Stacked bar plot & e_connect with e_river #597

Open MrMisc opened 7 months ago

MrMisc commented 7 months ago

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 commented 7 months ago

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

MrMisc commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

rdatasculptor commented 7 months ago

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

MrMisc commented 7 months ago

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 commented 7 months ago

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