JohnCoene / echarts4r

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

Question on emphasis settings, e_river & e_connect #596

Open MrMisc opened 7 months ago

MrMisc commented 7 months ago

In hopes of keeping the dataset to the same set of issues, I tried to make this into 1 post - I hope that is okay.

The illustrating example I have is as follows for filled_data.csv as the dataset.

filled_data<-read.csv("filled_data.csv")
filled_data <- filled_data[order(filled_data$groups, filled_data$dates), ]
filled_data|>group_by(groups)|>
  e_charts(dates) |>
  e_area(values,
         emphasis = list(
           focus = "self",blurScope = 'global', label = list(distance = 20)
         )) |> 
  e_y_axis(min = 0)|>
  e_tooltip()  |>
  e_theme("westeros")|>
  e_datazoom(
    type = "slider",
    toolbox = TRUE,
    bottom = 10
  )|>
  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")|>
  e_legend_unselect("marker")|>
  e_legend_unselect("Host 0[*]")

#Stream - part that is not working

filled_data|>group_by(groups)|>
  e_charts(dates) |>
  e_river(values) |> 
  e_tooltip()  |>
  e_theme("westeros")|>
  e_datazoom(
    type = "slider",
    toolbox = TRUE,
    bottom = 10
  )|>
  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")|>
  e_legend_unselect("marker")|>
  e_legend_unselect("Host 0[*]")

I essentially have 3 issues I would really appreciate some help on:

  1. How do I change the emphasis effect in say an e_area curve to for instance, have more blur, make the other sets have even less opacity, or move the series in question more (if possible)? I am getting the impression that echarts has those features from here even though I acknowledge this is for the series-bar. Is there something similar for something like e_area?

  2. In my running example, for whatever reason the e_river plot does not appear to work. Is there a reason for that? I initially reasoned that it was probably due to the volume of the dataset, but after trying it recently for other datasets, I do not quite understand what might be causing it now.

  3. I haven't had much success with this in other examples, and since the river plot doesn't even work here, I haven't considered trying it here, but is anyone here able to perhaps show me an instance of using e_connect as described here? I vaguely recall a slightly different example existing here, but it showed a really nice example of 2 linked charts. I would like to see if it is possible to do so with the 2 plots I am aspiring to build here (1 done, the 2nd stream one being my running issue)?

Thank you for your time!

rdatasculptor commented 7 months ago

The thing with e_river is that you need real dates instead of integers. It could be group_by works, not sure, but I followed this example with your data:

library(dplyr)
library(tidyr)
filled_data2 <- filled_data %>%
  select(groups, dates, values) %>%
  pivot_wider(names_from = "groups", values_from = "values") %>%
  mutate(dates = seq.Date(Sys.Date() - 30, Sys.Date(), by = "day")[1:n()])

filled_data2 |>#group_by(groups)|>
  e_charts(dates) |>
  e_river(`Egg -> Faeces`) |> 
  e_river(`Faeces -> Host`) |> 
e_river(`Faeces -> Host[*]`) |> 
e_river(`Faeces <-> Faeces`) |> 
  e_river(`Host -> Egg`) |> 
e_river(`Host <-> Host`) |> 
e_river(`Host 0[*]` ) |>
        e_river(marker) |> 
  e_tooltip()  |>
  e_theme("westeros")

Regarding the other issues (usually it is better to address one problem or question per issue, instead of three... :)), I am not entirely sure what your emphasis question is. For the other question, the one about connecting charts: I think it is better to make an example with two working charts first. Then we can take a look at how to connect them. Thanks!

MrMisc commented 7 months ago

@rdatasculptor Ah, that would be better then yeah? Gotcha. I will give the connecting thing a go again and be sure to open a separate issue for that. Thank you for the clarification and the help! I appreciate it!

For some reason, the following does not work. Assuming that I have followed your example here correctly,


filled_data|>mutate(dates = seq.Date(Sys.Date() - 30, Sys.Date(), by = "day")[1:n()])|>group_by(groups)|>e_charts(dates)|>
  e_river(values)

Yet, I get the same sort of issue where the river plot simply does not show. I can see some labels, but no actual streams of data.

rdatasculptor commented 7 months ago

The thing is I removed the whole group_by thing. Instead I made an e_river of each of your group variable.

What happens if you copy my exact code and run it?

MrMisc commented 7 months ago

It works perfectly when that is done. But I was wondering why that wouldn't work if the x-axis has already been readjusted to be dates?

Funnily I just realised, the dates thing should ideally be in hours since the time here is in hours XD

rdatasculptor commented 7 months ago

What happens if you put it in this order:

filled_data|>group_by(groups)|>mutate(dates = seq.Date(Sys.Date() - 30, Sys.Date(), by = "day")[1:n()])|>e_charts(dates)|> e_river(values)

JohnCoene commented 7 months ago

Can we have a small MRE for this?

MrMisc commented 6 months ago

Can we have a small MRE for this?

At the moment when I make MREs for this, the e_river issue cannot be replicated. I do not understand why that is the case. For example, if I make a faux dataset like so


df_eg <- data.frame(
  Year = rep(1980:2021, each = 6),
  Divorce_Amt = sample(800:2300, 252, replace = TRUE),
  Category = rep(c("  Under 5 Years (Number)", "  5-9 Years (Number)", "  10-14 Years (Number)", "  15-19 Years (Number)", "  20-24 Years (Number)", "  25-29 Years (Number)", "  30 Years & Over (Number)"), times = 36)
)

df_eg %>% mutate(Year = as.Date(paste0(df_eg$Year, "-01-01"))) |> group_by(Category) |> 
  e_charts(Year) |>
  e_river(Divorce_Amt)|>
  e_tooltip(emphasis = list(focus = "self"))|>
  e_legend(right = 5,top = 30,selector = "inverse",show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0)),focus = "series"),type = "scroll",width = 900,orient = "horizontal")|>
  e_title("Distribution of duration of anulled marriages over time","5 year resolutions")

No problem. However, if I use the actual dataset based off of this dataset

df<-read.csv("dataset.csv")

df %>% mutate(Year = as.Date(paste0(df$Year, "-01-01"))) |> group_by(Category) |> 
  e_charts(Year) |>
  e_river(Divorce_Amt)|>
  e_tooltip(emphasis = list(focus = "self"))|>
  e_legend(right = 5,top = 30,selector = "inverse",show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0)),focus = "series"),type = "scroll",width = 900,orient = "horizontal")|>
  e_title("Distribution of duration of anulled marriages over time","5 year resolutions")

river just breaks, like the initial example I showed. Sorry, I have tried but I do not know what distinguishes the dataset I am providing versus the faux dataset I fabricated that makes river work somehow.

munoztd0 commented 6 months ago

Same here, repeated measures problem I guess.


library(echarts4r)
library(tidyverse)

df<-read.csv("dataset.csv")

df_unique <- df |> 
  mutate(Year = as.Date(paste0(df$Year, "-01-01"))) |>
  group_by(Category, Year) |> 
  summarise(Divorce_Amt = mean(Divorce_Amt)) |>
  ungroup()

plot <- df_unique |>
  group_by(Category) |>
  e_charts(Year) |>
  e_river(Divorce_Amt)|>
  e_tooltip(emphasis = list(focus = "self"))|>
  e_legend(right = 5,top = 30,selector = "inverse",show=TRUE,icon = 'circle',emphasis = list(selectorLabel = list(offset = list(10,0)),focus = "series"),type = "scroll",width = 900,orient = "horizontal")|>
  e_title("Distribution of duration of anulled marriages over time","5 year resolutions")

image

is that better ?