gpt4 book ai didi

r - 在 R 中按天绘制时间间隔

转载 作者:行者123 更新时间:2023-12-04 10:44:07 25 4
gpt4 key购买 nike

我有一个 nx2每行包含某个时间间隔的开始和结束的数据帧。

              start                 end
3 2015-04-21 20:57:23 2015-04-21 23:55:23
5 2015-04-22 00:16:26 2015-04-22 00:28:23
8 2015-04-22 01:20:14 2015-04-22 02:34:51
12 2015-04-22 03:31:27 2015-04-22 04:31:03
14 2015-04-22 04:35:56 2015-04-22 05:54:10
16 2015-04-22 06:01:35 2015-04-22 07:14:35

我想在 R 中绘制这些间隔,x 轴为 24 小时,y 轴为天。所以它应该看起来像这样:



这样做的合适方法是什么?

这是前 20 行的 dput:
nx2 <- structure(list(start = structure(c(1429642643.153, 1429654586.936, 
1429658414.048, 1429666287.926, 1429670156.358, 1429675295.181,
1429680010.595, 1429691755.137, 1429700624.139, 1429708239.758,
1429712967.668, 1429716686.314, 1429725292.357, 1429735963.053,
1429741262.112, 1429751626.278, 1429755842.324, 1429774600.104,
1429787329.274, 1429791813.291), class = c("POSIXct", "POSIXt"
), tzone = ""), end = structure(c(1429653323.153, 1429655303.968,
1429662891.218, 1429669863.373, 1429674850.654, 1429679675.181,
1429686495.759, 1429695163.947, 1429707547.129, 1429711839.758,
1429715565.467, 1429722206.314, 1429734763.081, 1429740231.567,
1429749242.112, 1429752556.557, 1429767902.324, 1429783522.586,
1429790609.476, 1429795064.659), class = c("POSIXct", "POSIXt"
), tzone = "")), .Names = c("start", "end"), row.names = c(3L,
5L, 8L, 12L, 14L, 16L, 18L, 24L, 28L, 30L, 33L, 35L, 38L, 42L,
44L, 48L, 52L, 59L, 65L, 68L), class = "data.frame")

最佳答案

尝试这个:

library(lubridate)
library(ggplot2)

#data prep for plotting
nx2$DD <- day(nx2$start)
nx2$startHH <- hour(nx2$start)
nx2$endHH <- hour(nx2$end)

#plot
ggplot(nx2,aes(xmin = startHH, xmax = endHH + 1,
ymin = DD-0.5, ymax = DD + 0.5)) +
geom_rect(col="black",fill="blue",alpha=0.3) +
scale_x_continuous(breaks=0:24,limits=c(0,24)) +
scale_y_continuous(breaks=0:31,limits=c(0,31)) +
xlab("Hours") +
ylab("Days") +
theme_bw()

enter image description here

关于r - 在 R 中按天绘制时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30110258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com