如何使用echarts4r在日历中自动分隔年份

时间:2018-10-26 08:26:50

标签: r calendar visualization

我正在尝试使用calendar软件包制作echarts4r

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

但是这一年并没有策划2018年。
如何在calendar中设置自动分隔的年份? 是fill中的ggplot之类的解决方案吗?

预期输出:this

1 个答案:

答案 0 :(得分:1)

该API笨拙且不直观,但确实可行。您需要像现在一样添加两个日历,并在e_heatmap函数中引用它们的索引(以便根据正确的日历绘制热图)。另外,我使用e_data来传递第二个日历的值(x)。确保调整日历的位置,以使其不重叠(即: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) 

更新

0.2.0版本开始,可以通过按年份对数据进行分组来完成上述操作,这更加清晰和容易:

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