如何创建一个栅格图,显示行为的连续持续时间(无刻度)。

问题描述 投票:1回答:1

我需要创建栅格图来显示基于时间的数据,以下是我的数据的一个例子。对于每个参与者,我有24小时内他们在不同地点(婴儿床、地板等)度过的持续时间(开始和结束)。我想要一种能够连续显示参与者位置的栅格类型,而不是以刻度来显示。我如何在ggplot2中编写这个脚本?

id  onset       offset     location
1   06:00:00.0  06:35:00.0  hichair
1   06:35:00.0  08:00:00.0  carseat
1   08:00:00.0  09:00:00.0  chifurn
1   09:00:00.0  09:45:00.0  cribcradle
1   09:45:00.0  12:00:00.0  gndfl
1   12:00:00.0  12:45:00.0  chifurn
1   12:45:00.0  15:00:00.0  gndfl
1   15:00:00.0  16:00:00.0  chifurn
1   16:00:00.0  17:15:00.0  strowlkr
1   17:15:00.0  18:00:00.0  carseat
1   18:00:00.0  18:30:00.0  hichair
1   18:30:00.0  19:30:00.0  gndfl
1   19:30:00.0  20:00:00.0  adlfurn
1   20:00:00.0  06:00:00.0  cribcradle
2   06:00:00.0  06:30:00.0  cribcradle
2   06:30:00.0  06:45:00.0  adlfurn
2   06:45:00.0  06:55:00.0  adlfurn
2   06:55:00.0  07:45:00.0  gndfl
2   07:45:00.0  07:55:00.0  arms
2   07:55:00.0  08:30:00.0  gndfl
2   08:30:00.0  08:50:00.0  hichair
2   08:50:00.0  09:45:00.0  strowlkr
2   09:45:00.0  10:00:00.0  cribcradle
2   10:00:00.0  11:00:00.0  cribcradle
2   11:00:00.0  11:05:00.0  cribcradle
2   11:05:00.0  11:15:00.0  hichair
2   11:15:00.0  11:35:00.0  infcarry
2   11:35:00.0  12:30:00.0  gndfl
2   12:30:00.0  13:00:00.0  hichair
2   13:00:00.0  13:30:00.0  gndfl
2   13:30:00.0  15:15:00.0  cribcradle
2   15:15:00.0  16:20:00.0  carseat
2   16:20:00.0  18:30:00.0  arms
2   18:30:00.0  18:45:00.0  infcarry
2   18:45:00.0  19:45:00.0  carseat
2   19:45:00.0  20:05:00.0  arms
2   20:05:00.0  06:00:00.0  cribcradle
3   06:00:00.0  07:00:00.0  adlfurn
3   07:00:00.0  07:05:00.0  adlfurn
3   07:05:00.0  07:45:00.0  arms
3   07:45:00.0  08:00:00.0  gndfl
3   08:00:00.0  08:15:00.0  hichair
3   08:15:00.0  08:30:00.0  adlfurn
3   08:30:00.0  08:45:00.0  strowlkr
3   08:45:00.0  09:00:00.0  arms
3   09:00:00.0  11:30:00.0  gndfl
3   11:30:00.0  12:30:00.0  hichair
3   12:30:00.0  15:00:00.0  cribcradle
3   15:00:00.0  15:25:00.0  hichair
3   15:25:00.0  17:00:00.0  gndfl
3   17:00:00.0  17:15:00.0  carseat
3   17:15:00.0  17:30:00.0  hichair
3   17:30:00.0  17:45:00.0  gndfl
3   17:45:00.0  18:05:00.0  carseat
3   18:05:00.0  18:25:00.0  arms
3   18:25:00.0  18:40:00.0  carseat
3   18:40:00.0  19:10:00.0  gndfl
3   19:10:00.0  19:25:00.0  hichair
3   19:25:00.0  19:45:00.0  carseat
3   19:45:00.0  19:50:00.0  arms
3   19:50:00.0  19:55:00.0  adlfurn
3   19:55:00.0  20:05:00.0  arms
3   20:05:00.0  20:10:00.0  adlfurn
3   20:10:00.0  20:15:00.0  arms
3   20:15:00.0  20:20:00.0  adlfurn
3   20:20:00.0  20:25:00.0  arms
3   20:25:00.0  03:30:00.0  cribcradle
3   03:30:00.0  04:00:00.0  arms
3   04:00:00.0  06:00:00.0  adlfurn

下面是一篇发表的文章中的一个例子,说明了我想要的东西(但在我的例子中,我有多种颜色来描述不同的位置)。

这是我目前拥有的脚本,它使用地点的onsets提供了一条不连续的时间线。我还需要对图中的时间进行排序,以显示从当前早上6:00到第二天早上6:00的数据。我希望这些条形图是连续的。

谢谢你的帮助!

ggplot(loc_us) +
  geom_raster(aes(x=onset, y=id, fill = location)) +
  coord_equal() + theme_classic() +
  theme(legend.position="bottom") +
  scale_x_discrete(breaks=c("00:00:00.0", "06:00:00.0", "12:00:00.0", "20:00:00.0"))

我的脚本给我的输出的照片

ggplot2 raster
1个回答
0
投票

通过转换 onsetoffset 到日期时间,这可以通过 geom_rect. 棘手的部分是转换为日期时间和设置正确的日期。对于日期时间的转换,我首先使用 lubridate::hms 然后通过 lubridate::as_datetime. 其次,由于日期时间转换将1970年1月1日作为日期,我们必须在第二步中对日期进行修正,即在时间上增加一天,使之与 00:00:0006:00:00 或如果 offset 时间&gt。onset 的时间。

library(ggplot2)
library(dplyr)

loc_us %>% 
  mutate(
    # convert to datetime
    onset = lubridate::as_datetime(lubridate::hms(onset)), 
    offset = lubridate::as_datetime(lubridate::hms(offset)),
    # set correct dates
    onset = case_when(
      onset < lubridate::as_datetime(lubridate::hms("06:00:00")) ~ onset  + lubridate::days(1),
      TRUE ~ onset),
    offset = case_when(
      offset < onset ~ offset + lubridate::days(1),
      TRUE ~ offset)
    ) %>% 
  ggplot() +
  geom_rect(aes(xmin=onset, xmax = offset, ymin=id-.4, ymax = id + .4, fill = location), color = "white") +
  scale_x_datetime(date_labels = "%H:%M") +
  theme_classic() +
  theme(legend.position="bottom")

创建于2020-05-25,由 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.