JohnCoene / echarts4r

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

e_river #605

Closed MrMisc closed 6 months ago

MrMisc commented 6 months ago

I would like someone to help me understand, if possible, what makes river tick. I was under the impression that say I want to do time series, that I need to make sure that the column to be used for the x-axis be of Date format. However, my river plots often break and here is an MRE

library(echarts4r)
# Creating sample data for demonstration
Years <- sample(2000:2020, 942, replace = TRUE)
Sub_Continent <- sample(c("Africa", "Asia", "Europe", "North America", "South America"), 942, replace = TRUE)
Country <- sample(c("Country_A", "Country_B", "Country_C", "Country_D", "Country_E"), 942, replace = TRUE)
n <- sample(1:20, 942, replace = TRUE)
Cases <- sample(1:1000, 942, replace = TRUE)
Deaths <- sample(1:500, 942, replace = TRUE)

# Creating the tibble
eg <- tibble(Year = Years, Sub_Continent, Country, n, Cases, Deaths)

#Works
eg |>e_charts(Year) |> e_river(n)

#Works
eg%>% mutate(Year = as.Date(paste0(eg$Year, "-01-01"))) |>e_charts(Year) |> e_river(n)

#Doesn't work
eg %>% group_by(Sub_Continent) |> e_charts(Year,timeline = F)|>
  e_river(n)

#Doesn't work
eg %>% mutate(Year = as.numeric(Year)) %>% group_by(Sub_Continent) |> e_charts(Year,timeline = F)|>
  e_river(n)

#Doesn't work
eg %>% mutate(Year = as.Date(paste0(eg$Year, "-01-01"))) %>% group_by(Sub_Continent) |> e_charts(Year,timeline = F)|>
  e_river(n)

#Timelines 
#Doesn't work
eg %>% group_by(Sub_Continent) |> e_charts(Year,timeline = T)|>
  e_river(n)
munoztd0 commented 6 months ago

image

here is your problem you have repeated measures in your group_by. We need one value per group per year.


library(echarts4r)
library(tidyverse)

# Creating sample data for demonstration
Years <- sample(2000:2020, 942, replace = TRUE)
Sub_Continent <- sample(c("Africa", "Asia", "Europe", "North America", "South America"), 942, replace = TRUE)
Country <- sample(c("Country_A", "Country_B", "Country_C", "Country_D", "Country_E"), 942, replace = TRUE)
n <- sample(1:20, 942, replace = TRUE)
Cases <- sample(1:1000, 942, replace = TRUE)
Deaths <- sample(1:500, 942, replace = TRUE)

# Creating the tibble
eg <- tibble(Year = Years, Sub_Continent, Country, n, Cases, Deaths) |>
  mutate(Year = as.Date(paste0(eg$Year, "-01-01")))

# here we have to make a choice because you have 
#contradictory/repeated information in your data
df_unique <- eg |>
  group_by(Sub_Continent, Year) |>
  summarise(mean = mean(n)) # for example but you have too choose something
# you can also filter by country or take the max

#now everything works

plot <- df_unique |>
  group_by(Sub_Continent) |>
  e_charts(Year) |>
  e_river(mean) |>
  e_tooltip(trigger = "axis")

plot

image

Created on 2023-12-21 with reprex v2.0.2.9000

here is my buy me a coffee link if you feel like it

MrMisc commented 6 months ago

Thank you! I will be sure to double check my actual dataset and recall this!