R,在Shiny和ggvis中绘制日期的问题

时间:2015-11-17 23:54:03

标签: r shiny ggvis posixct

我对Shiny和ggvis有点新鲜,实际上这里有几个问题。首先,我有以下格式的数据:

            Event.Hour Count    Group
1  2015-10-08 00:00:00  1476      All
2  2015-10-08 01:00:00 22186      All
3  2015-10-08 02:00:00  2958      All
4  2015-10-08 03:00:00  3484      All
5  2015-10-08 04:00:00  4159      All
6  2015-10-08 05:00:00  2960      All
7  2015-10-08 00:00:00   222 Business
8  2015-10-08 01:00:00    26 Business
9  2015-10-08 02:00:00   542 Business
10 2015-10-08 03:00:00    26 Business
11 2015-10-08 04:00:00  4760 Business
12 2015-10-08 05:00:00  4640 Business
13 2015-10-08 00:00:00  1704   System
14 2015-10-08 01:00:00   100   System
15 2015-10-08 02:00:00   122   System
16 2015-10-08 03:00:00    10   System
17 2015-10-08 04:00:00  3339   System
18 2015-10-08 05:00:00  5370   System

日期是使用lubridate的dmy_hms函数创建的:

> class(viz_set$Event.Hour)
[1] "POSIXct" "POSIXt" 

重新创建数据集的代码:

library(ggvis)
library(dplyr)
library(lubridate)

zz<- "Event.Hour Count    Group
1  2015-10-08-00:00:00  1476      All
2  2015-10-08-01:00:00 22186      All
3  2015-10-08-02:00:00  2958      All
4  2015-10-08-03:00:00  3484      All
5  2015-10-08-04:00:00  4159      All
6  2015-10-08-05:00:00  2960      All
7  2015-10-08-00:00:00   222 Business
8  2015-10-08-01:00:00    26 Business
9  2015-10-08-02:00:00   542 Business
10 2015-10-08-03:00:00    26 Business
11 2015-10-08-04:00:00  4760 Business  
12 2015-10-08-05:00:00  4640 Business
13 2015-10-08-00:00:00  1704   System
14 2015-10-08-01:00:00   100   System
15 2015-10-08-02:00:00   122   System
16 2015-10-08-03:00:00    10   System
17 2015-10-08-04:00:00  3339   System
18 2015-10-08-05:00:00  5370   System"

viz_set<-read.table(text=zz, header = T)

viz_set$Event.Hour <- ymd_hms(viz_set$Event.Hour)

然后我将数据绘制为ggvis对象并向交互式视觉添加工具提示:

getData1 <- function(dat){
  paste(paste("Value:", dat$Count, "<br />"),
        paste("Time:", dat$Event.Hour))
} 

viz_set %>%
  ggvis(~Event.Hour, ~Count, stroke = ~Group) %>% 
  filter(Group %in% eval(input_checkboxgroup(c("All", "Business", "System"), select = "All"))) %>%
  layer_lines(strokeWidth := 2) %>%
  scale_datetime(property = "x", nice = "hour") %>%
  layer_points(opacity = 0, fill = ~Group) %>%
  add_tooltip(getData1, on = "click") %>%
  add_axis("x", subdivide = 5, tick_size_major=10, tick_size_minor = 5, title = "Event.Hour", title_offset = 30) %>%
  add_axis("y", title = "Count", title_offset = 60)

生成的工具提示以Unix纪元格式显示日期,我无法弄清楚如何将其显示为字符。

在getData1函数中我尝试过:

as.character(dat$Event.Hour)

as.POSIXct(dat$Event.Hour, origin = '1970-01-01')

无济于事。

我还尝试添加一个日期为字符串的新列,并使用该列上的工具提示。两者都没有奏效。

其次,在生成的ggvis图中,X轴日期时间与值不对齐。数据似乎应该在几个小时之前开始和结束:

Here you can see that the X-axis is off by a few hours.

第一个数据点应该从08年10月开始,并应该在周三14点结束。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

看起来时区可能会导致移位的轴。一个简单的解决方法是简单地将ymd_dms中的时区设置为系统的时区。在那之后,对我而言看起来很合理。

(mytz = Sys.timezone())
[1] "America/Los_Angeles"

viz_set$Event.Hour = ymd_hms(viz_set$Event.Hour, tz = mytz)

就工具提示而言,以毫秒为单位解决问题的方法是简单地显示原始数据集中的数据,而不是通过ggvis显示的数据集。您可以使用key执行此操作。这涉及创建一个唯一表示原始数据集中每一行的id变量。

viz_set$id = 1:nrow(viz_set)

现在工具提示功能可能如下所示。

getData2 = function(dat){
    if(is.null(dat$id)) return(NULL)
    row = viz_set[viz_set$id == dat$id, ]
    paste(paste("Value:", row$Count, "<br />"),
         paste("Time:", as.character(row$Event.Hour)))
} 

请注意,与帮助文档不同,该函数的第一行涉及dat$id而不仅仅是dat。当我在这里使用dat时,如果我错误地点击了该行而不是该点,我有时会显示空白的工具提示信息。我的替代方案似乎解决了这个问题。

现在只需将key := ~id添加到layer_points即可从实际数据集中弹出工具提示值。

viz_set %>%
    ggvis(~Event.Hour, ~Count, stroke = ~Group) %>% 
    filter(Group %in% eval(input_checkboxgroup(c("All", "Business", "System"), select = "All"))) %>%
    layer_lines(strokeWidth := 2) %>%
    scale_datetime(property = "x", nice = "hour") %>%
    layer_points(opacity := 0, fill = ~Group, key := ~id) %>%
    add_tooltip(getData2, on = "click") %>%
    add_axis("x", subdivide = 5, tick_size_major=10, tick_size_minor = 5, title = "Event.Hour", title_offset = 30) %>%
    add_axis("y", title = "Count", title_offset = 60)