JohnCoene / echarts4r

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

e_calendar doesn't plot multiples date ranges properly #517

Open arrietafernando opened 1 year ago

arrietafernando commented 1 year ago

When plotting for multiple years when laid multiple calendars grouped by year, the resulting chart is partially displayed if all year ranges are not provided in the e_calendars elements.

echarts4r: Version: 0.4.4

From the documentation example code: https://echarts4r.john-coene.com/articles/chart_types.html#calendar:

The following (one range) is showed ok:

dates <- seq.Date(as.Date("2017-01-01"), as.Date("2019-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)

year <- data.frame(date = dates, values = values)

year |> 
  e_charts(date) |> 
  e_calendar(range = "2018") |> 
  e_heatmap(values, coord_system = "calendar") |> 
  e_visual_map(max = 30) |> 
  e_title("Calendar", "Heatmap")

The following with all date ranges (2017, 2018, 2019) is showed also ok:

year |> 
  dplyr::mutate(year = format(date, "%Y")) |> # get year from date
  group_by(year) |> 
  e_charts(date) |> 
  e_calendar(range = "2017",top="40") |> 
  e_calendar(range = "2018",top="260") |> 
  e_calendar(range = "2019",top="460") |> 
  e_heatmap(values, coord_system = "calendar") |> 
  e_visual_map(max = 30) |> 
  e_title("Calendar", "Heatmap")|>
  e_tooltip("item") 

But the next one only plot the first range (2017) and the other (2018) in blank:

year |> 
  dplyr::mutate(year = format(date, "%Y")) |> # get year from date
  group_by(year) |> 
  e_charts(date) |> 
  e_calendar(range = "2017",top="40") |> 
  e_calendar(range = "2018",top="260") |> 
  e_heatmap(values, coord_system = "calendar") |> 
  e_visual_map(max = 30) |> 
  e_title("Calendar", "Heatmap")|>
  e_tooltip("item") 

Could you tell me if it is a bug or if it is necessary to provide only the date ranges that you want to display?

Thanks

munoztd0 commented 1 year ago

Hi,

Yeah it's kinda of a mess because the function breaks with unused data.

A quick fix is to remove the unused dates (or to select the used ones).

dates <- seq.Date(as.Date("2017-01-01"), as.Date("2019-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)

year <- data.frame(date = dates, values = values)

plot2017_2018 <- year |> 
                    dplyr::mutate(year = format(date, "%Y")) |> # get year from date
                    group_by(year) |>
                    filter(!year == "2019") |> #need to remove the unused year or select only the used ones
                    e_charts(date) |> 
                    e_calendar(range = "2017",top="40") |> 
                    e_calendar(range = "2018",top="260") |> 
                    e_heatmap(values, coord_system = "calendar") |> 
                    e_visual_map(max = 30) |> 
                    e_title("Calendar", "Heatmap")|>
                    e_tooltip("item") 
#> Error in e_tooltip(e_title(e_visual_map(e_heatmap(e_calendar(e_calendar(e_charts(filter(group_by(dplyr::mutate(year, : could not find function "e_tooltip"

Created on 2023-06-13 with reprex v2.0.2.9000

image