绘制包含完整时间序列的条形图,尽管R中存在缺失值

eiee3dmh  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(107)

我正在处理一个数据集,从中选择了几行和几列

testfile <- structure(list(date = c("2023-03-30T08:00:00.000Z", "2023-03-30T09:00:00.000Z", 
"2023-03-30T10:00:00.000Z", "2023-03-30T11:00:00.000Z", "2023-03-30T12:00:00.000Z", 
"2023-03-30T13:00:00.000Z", "2023-03-30T14:00:00.000Z", "2023-03-30T15:00:00.000Z", 
"2023-03-30T16:00:00.000Z", "2023-03-30T17:00:00.000Z", "2023-03-31T06:00:00.000Z", 
"2023-03-31T07:00:00.000Z", "2023-03-31T08:00:00.000Z", "2023-03-31T09:00:00.000Z", 
"2023-03-31T10:00:00.000Z", "2023-03-31T11:00:00.000Z", "2023-03-31T12:00:00.000Z", 
"2023-03-31T13:00:00.000Z", "2023-03-31T14:00:00.000Z", "2023-03-31T15:00:00.000Z", 
"2023-03-31T16:00:00.000Z", "2023-03-31T17:00:00.000Z", "2023-04-01T06:00:00.000Z", 
"2023-04-01T07:00:00.000Z", "2023-04-01T08:00:00.000Z", "2023-04-01T09:00:00.000Z", 
"2023-04-01T10:00:00.000Z", "2023-04-01T11:00:00.000Z", "2023-04-01T12:00:00.000Z", 
"2023-04-01T13:00:00.000Z", "2023-04-01T14:00:00.000Z", "2023-04-01T15:00:00.000Z", 
"2023-04-01T16:00:00.000Z", "2023-04-01T17:00:00.000Z", "2023-04-02T06:00:00.000Z", 
"2023-04-02T07:00:00.000Z", "2023-04-02T08:00:00.000Z", "2023-04-02T09:00:00.000Z", 
"2023-04-02T10:00:00.000Z", "2023-04-02T11:00:00.000Z", "2023-04-02T12:00:00.000Z", 
"2023-04-02T13:00:00.000Z", "2023-04-02T14:00:00.000Z", "2023-04-02T15:00:00.000Z", 
"2023-04-02T16:00:00.000Z", "2023-04-02T17:00:00.000Z", "2023-04-03T06:00:00.000Z", 
"2023-04-03T07:00:00.000Z"), pedestrian = c(0, 2.004454343, 22.0155902004, 
26.0200445434, 20.0111358575, 7, 20.0178173719, 15, 51, 41, 25, 
25.008908686, 25.0222717149, 10, 22, 115, 152, 149.0645879733, 
95, 4, 108, 85, 19, 23.5985790409, 31, 42, 35, 24, 18, 34.7234042553, 
27.8252427184, 25, 20, 17, 0, 20.008908686, 28, 38.0670391061, 
42.6666666667, 20.0808080808, 22.0632054176, 28.3636363636, 27.6888361045, 
34, 24.0200445434, 32.3080407701, 10, 14)), row.names = c(NA, 
48L), class = "data.frame")

我如何绘制一个横坐标为完整时间尺度的testfile$pedestrian条形图?我应该开始用NA的代替缺失的时间吗?使用imputeTSggpplot2

编辑

是否有类似的解决方案与POSIXct对象一起工作?

testfile <- structure(list(date = structure(c(1680163200, 1680166800, 1680170400, 
1680174000, 1680177600, 1680181200, 1680184800, 1680188400, 1680192000, 
1680195600, 1680242400, 1680246000, 1680249600, 1680253200, 1680256800, 
1680260400, 1680264000, 1680267600, 1680271200, 1680274800, 1680278400, 
1680282000, 1680328800, 1680332400, 1680336000, 1680339600, 1680343200, 
1680346800, 1680350400, 1680354000, 1680357600, 1680361200, 1680364800, 
1680368400, 1680415200, 1680418800, 1680422400, 1680426000, 1680429600, 
1680433200, 1680436800, 1680440400, 1680444000, 1680447600, 1680451200, 
1680454800, 1680501600, 1680505200), class = c("POSIXct", "POSIXt"
), tzone = "Europe/Paris"), pedestrian = c(0, 2.004454343, 22.0155902004, 
26.0200445434, 20.0111358575, 7, 20.0178173719, 15, 51, 41, 25, 
25.008908686, 25.0222717149, 10, 22, 115, 152, 149.0645879733, 
95, 4, 108, 85, 19, 23.5985790409, 31, 42, 35, 24, 18, 34.7234042553, 
27.8252427184, 25, 20, 17, 0, 20.008908686, 28, 38.0670391061, 
42.6666666667, 20.0808080808, 22.0632054176, 28.3636363636, 27.6888361045, 
34, 24.0200445434, 32.3080407701, 10, 14)), row.names = c(NA, 
48L), class = "data.frame")
vmdwslir

vmdwslir1#

这个怎么样

with(transform(testfile, date=strptime(date, "%Y-%m-%dT%H:%M:%OSZ")), 
     plot(date, pedestrian, type='h', lwd=3, lend=2))

相关问题