jyjek
jyjek

Reputation: 2717

How to make auto-separated years in a calendar with echarts4r?

I'm trying to make calendar with echarts4r package.

library(tidyverse)
library(echarts4r)

dates <- seq.Date(as.Date("2017-01-01"), as.Date("2018-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)
year <- data.frame(date = dates, values = values)

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")

But this one didn't plot 2018 year.
How to make auto-separated years in a calendar? Is any solution like fill from ggplot?

Expected output : this

Upvotes: 0

Views: 226

Answers (1)

JohnCoene
JohnCoene

Reputation: 2261

The API is admittedly clunky and unintuitive but it is doable. You need to add the two calendars as you do already, reference their index in your e_heatmap function (so that the heatmaps is plotted against the correct calendar). Also, I use e_data in order to pass the values (x) for the second calendar. Make sure to adjust to position of the calendars so that they do not overlap (i.e.: top = 300).

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

df <- data.frame(date18 = dates18, date17 = dates17, values = values)

df %>% 
    e_charts(date18) %>% 
    e_calendar(range = "2018") %>% 
    e_heatmap(values, coord.system = "calendar", calendarIndex = 0, name = "2018") %>% 
    e_data(df, date17) %>% 
    e_calendar(range = "2017", top = 300) %>% 
    e_heatmap(values, coord.system = "calendar", calendarIndex = 1, name = "2017") %>%
    e_visual_map(max = 30) 

Update

Since version 0.2.0 the above can be done by grouping the data by year which is much clearer and easier:

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

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

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") 

Upvotes: 1

Related Questions