如何在R轴中绘制箭头,其中x轴是时间轴?

时间:2015-04-30 15:23:14

标签: r datetime plot

我想在绘图中绘制垂直箭头,x轴必须是时间轴。 箭头必须从my_data$from转到my_data$to

我尝试了这个片段,但没有绘制箭头。

my_data <- data.frame(timestamp=c("11:20:50","15:07:59","17:44:02"),from=c(0,1,2),to=c(1,3,1))
my_data$timestamp <- strptime(my_data$timestamp, "%H:%M:%S")
midnight <- strptime("00:00:00","%H:%M:%S")
plot(my_data$timestamp,my_data$from)
arrows(x0=my_data$timestamp-midnight,y0=my_data$from,x1=my_data$timestamp-midnight,y1=my_data$to,col='red')

enter image description here

我错过了什么?

1 个答案:

答案 0 :(得分:2)

尝试此操作并根据需要进行修改:

with(my_data, arrows(x0 = as.numeric(timestamp), y0 = from, y1 = to, col = 'red'))

例如:

my_data <- data.frame(
  timestamp = as.POSIXct(c("11:20:50", "15:07:59","17:44:02"), format = "%H:%M:%S"),
  from = c(0,1,2),
  to = c(1,3,1))

plot(to ~ timestamp, my_data, type = 'n', ylim = range(from, to))
with(my_data, arrows(x0 = as.numeric(timestamp), y0 = from, y1 = to, col = 'red'))

screenshot