我想在绘图中绘制垂直箭头,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')
我错过了什么?
答案 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'))