如何创建显示连续持续时间的行为的光栅图(无刻度)
how to create a raster plot displaying continuous durations of time for behavior (without ticks)
我需要创建显示基于时间的数据的光栅图。
这是我拥有的数据的示例。对于每个参与者,我有他们在一天 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
这是我想要的一篇已发表文章的示例(但在我的例子中,我会用多种颜色描绘不同的位置)
这是我目前拥有的脚本,它使用位置的开始提供了一个不连贯的时间线。我还需要对图中的时间进行排序,显示从6:00am当前到第二天6:00am的数据。我希望条形是连续的。
感谢您的帮助!!
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"))
我的脚本给我的输出照片
通过将 onset
和 offset
转换为日期时间,这可以通过 geom_rect
实现。棘手的部分是转换为日期时间并设置正确的日期。对于日期时间转换,我首先使用 lubridate::hms
转换为句点,然后通过 lubridate::as_datetime
转换为日期时间。其次,由于日期时间转换将 1970 年 1 月 1 日添加为日期,我们必须在第二步中更正日期,即在 00:00:00
和 06:00:00
之间或如果 offset
之间添加一天时间 > 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")
由 reprex package (v0.3.0)
于 2020-05-25 创建
我需要创建显示基于时间的数据的光栅图。 这是我拥有的数据的示例。对于每个参与者,我有他们在一天 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
这是我想要的一篇已发表文章的示例(但在我的例子中,我会用多种颜色描绘不同的位置)
这是我目前拥有的脚本,它使用位置的开始提供了一个不连贯的时间线。我还需要对图中的时间进行排序,显示从6:00am当前到第二天6:00am的数据。我希望条形是连续的。
感谢您的帮助!!
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"))
我的脚本给我的输出照片
通过将 onset
和 offset
转换为日期时间,这可以通过 geom_rect
实现。棘手的部分是转换为日期时间并设置正确的日期。对于日期时间转换,我首先使用 lubridate::hms
转换为句点,然后通过 lubridate::as_datetime
转换为日期时间。其次,由于日期时间转换将 1970 年 1 月 1 日添加为日期,我们必须在第二步中更正日期,即在 00:00:00
和 06:00:00
之间或如果 offset
之间添加一天时间 > 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")
由 reprex package (v0.3.0)
于 2020-05-25 创建