我的数据如下:
Date Timestamp xAcc yAcc zAcc
17:42:20.361 1866703 -0.03526625 -0.01266312 -0.9679290
17:42:20.381 1866713 -0.02735012 -0.02082901 -0.9759240
17:42:20.381 1866723 -0.05109851 -0.01674607 -0.9599340
我想要实现的是,将 Timestamp 绘制为例如 xAcc ,但是在x刻度上以秒为单位的实时,从0开始。
diff = as.difftime("00:00:00.010001", "%H:%M:%OS")
start = as.POSIXct("1970-01-01 00:00:00.000", "", format="%Y-%m-%d %H:%M:%OS")
DF$Date = seq.POSIXt(start, by=diff, length.out=length(DF$Date))
ggplot(DF, aes(Timestamp, xAcc)) +
geom_line() +
labs(x = 't') +
scale_x_datetime(labels = DF$Date)
但后来我明白了:
Error: Invalid input: time_trans works with objects of class POSIXct only
我发现这很奇怪,因为?seq.POSIXt说
值“POSIXct”类的向量。
问题是否与
有关> class(DF$Date)
[1] "POSIXct" "POSIXt"
我在combined use of geom_boxplot and scale_x_datetime找到了一个类似的问题,并尝试了不同的
变体... + scale_x_discrete(labels = format.Date(DF$Date, "%S"))
但那时根本就没有规模。
任何帮助都将不胜感激。
答案 0 :(得分:0)
您可以将Date
POSIXct
映射到x
。
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-11-14
library(ggplot2)
DF <- read.table(text = " Date Timestamp xAcc yAcc zAcc
17:42:20.361 1866703 -0.03526625 -0.01266312 -0.9679290
17:42:20.381 1866713 -0.02735012 -0.02082901 -0.9759240
17:42:20.381 1866723 -0.05109851 -0.01674607 -0.9599340", header = TRUE)
diff <- as.difftime("00:00:00.010001", "%H:%M:%OS")
start <- as.POSIXct("1970-01-01 00:00:00.000", "", format="%Y-%m-%d %H:%M:%OS")
DF$Date <- seq.POSIXt(start, by=diff, length.out=length(DF$Date))
p <- ggplot(DF, aes(Date, xAcc)) +
geom_line() +
labs(x = 't')
p
如果您想要更多地控制中断和标签,可以在scale_x_datetime()
上指定它们:
p + scale_x_datetime(breaks = DF$Date, labels = DF$Date)