我有一个包含各种传感器读数的CSV文件。
它看起来好像代表了四个不同的时间序列,温度,动作,功率和电池,每次读数的时间都略有不同。
dat <- read.csv(text = "id,device_id,type,value,timestamp
1432,4,temperature,21,2015-06-01T00:00:00Z
1433,4,motion,0,2015-06-01T00:00:15Z
1434,4,power,0,2015-06-01T00:00:30Z
1435,4,battery,4.16,2015-06-01T00:00:46Z
1448,4,temperature,21,2015-06-01T00:17:00Z
1449,4,motion,0,2015-06-01T00:17:15Z
1450,4,power,0,2015-06-01T00:17:30Z
1451,4,battery,4.16,2015-06-01T00:17:45Z
1464,4,temperature,21,2015-06-01T00:33:57Z")
str(dat)
'data.frame': 9 obs. of 5 variables:
$ id : int 1432 1433 1434 1435 1448 1449 1450 1451 1464
$ device_id: int 4 4 4 4 4 4 4 4 4
$ type : Factor w/ 4 levels "battery","motion",..: 4 2 3 1 4 2 3 1 4
$ value : num 21 0 0 4.16 21 0 0 4.16 21
$ timestamp: Factor w/ 9 levels "2015-06-01T00:00:00Z",..: 1 2 3 4 5 6 7 8 9
如何操纵数据以根据时间绘制四个图形?
答案 0 :(得分:5)
只需几行代码就可以在R中轻松完成此操作。
第一步是将时间戳转换为POSIXct日期。最简单的方法是在导入期间将此列强制为类character
:
dat <- read.csv(colClasses = c(timestamp = "character"),
text = "id,device_id,type,value,timestamp
1432,4,temperature,21,2015-06-01T00:00:00Z
1433,4,motion,0,2015-06-01T00:00:15Z
1434,4,power,0,2015-06-01T00:00:30Z
1435,4,battery,4.16,2015-06-01T00:00:46Z
1448,4,temperature,21,2015-06-01T00:17:00Z
1449,4,motion,0,2015-06-01T00:17:15Z
1450,4,power,0,2015-06-01T00:17:30Z
1451,4,battery,4.16,2015-06-01T00:17:45Z
1464,4,temperature,21,2015-06-01T00:33:57Z")
然后转换为POSIXct
。有关?strptime
(完整日期)和%F
(全职)的含义,请参阅%T
:
dat$timestamp <- as.POSIXct(dat$timestamp, format = "%FT%TZ")
str(dat)
'data.frame': 9 obs. of 5 variables:
$ id : int 1432 1433 1434 1435 1448 1449 1450 1451 1464
$ device_id: int 4 4 4 4 4 4 4 4 4
$ type : Factor w/ 4 levels "battery","motion",..: 4 2 3 1 4 2 3 1 4
$ value : num 21 0 0 4.16 21 0 0 4.16 21
$ timestamp: POSIXct, format: "2015-06-01 00:00:00" "2015-06-01 00:00:15" ...
然后使用ggplot2
进行绘图。例如:
library(ggplot2)
ggplot(dat, aes(x=timestamp, y = value, group = device_id)) +
geom_point() +
geom_line() +
facet_grid(type ~ ., scales = "free_y")