简体   繁体   中英

Plot Time on Y-Axis using R

How can we plot on the Y-axis the time of the day (eg: 09:15)?

Tried using as.POSIXct but it plots values 1.0 to 2.0...

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

在此处输入图片说明

Try this:

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

There's probably a better way to do this, but this seems to work:

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM