使用R在Y轴上绘制时间

时间:2016-07-18 14:55:11

标签: r

我们如何在Y轴上绘制一天中的时间(例如:09:15)?

尝试使用as.POSIXct,但它会将值1.0到2.0 ......

df <- data.frame(date = as.Date(timestamp),
                 time = format(as.POSIXct(timestamp), "%H:%M:%S")
)
plot(df)

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个:

plot(df,yaxt="n",las=2)
axis(2,1:length(levels(df$time)),labels=levels(df$time),las=2,cex.axis=.55)

答案 1 :(得分:0)

这可能是一种更好的方法,但这似乎有效:

gettime <- function(x) {Sys.sleep(2); as.POSIXct(Sys.time(), origin = "1970-01-01")}
times <- sapply(1:10, gettime)
times
# [1] 1468856662 1468856664 1468856666 1468856668 1468856670 1468856672
# [7] 1468856674 1468856676 1468856678 1468856680

df <- data.frame(date = as.POSIXct(times, origin = "1970-01-01"),
                 time = as.POSIXct(times, origin = "1970-01-01"))
par(mar = c(6, 6, 2, 2))
plot(df$time ~ df$date, axes = FALSE, ylab = "", xlab = "")

axis(side = 2, at = axTicks(side = 2), 
     labels = format(as.POSIXct(axTicks(side = 2),
                                origin = "1970-01-01"), 
                     "%H:%M:%S"), 
     las = 2)
axis(side = 1, at = axTicks(side = 1), 
     labels = format(as.POSIXct(axTicks(side = 1),
                                origin = "1970-01-01"), 
                     "%y-%m-%d"),
     las = 2)

enter image description here