简体   繁体   中英

Using plot abline() when x-axis is date format in r

I looking for making abline() works well when data is time series format(Date format) However, I cannot find any clear solution for this my data is as follows:

    X                                    Y
1   2018-04-01 00:00:34                  3.58
2   2018-04-01 00:01:38                  2.79
3   2018-04-01 00:02:41                 -2.42
4   2018-04-01 00:03:44                  4.60
5   2018-04-01 00:04:47                  6.05

and What I actually do is :

plot(y = df$Y, x = df$X)
abline(v = "2018-04-01 00:04:47", col = "red")
abline(v = "2018-04-01 00:03:44.000", col = "blue")

But It doesn't work(abline function), does anyone who can do this...?

Use as.POSIXct inside abline

plot(df$X, df$Y)
abline(v = as.POSIXct("2018-04-01 00:04:47"), col = "red")
abline(v = as.POSIXct("2018-04-01 00:03:44.000"), col = "blue")

在此处输入图片说明


Sample data

df <- read.table(text =
    "    X                                    Y
1   '2018-04-01 00:00:34'                  3.58
2   '2018-04-01 00:01:38'                  2.79
3   '2018-04-01 00:02:41'                 -2.42
4   '2018-04-01 00:03:44'                  4.60
5   '2018-04-01 00:04:47'                  6.05", header = T)

df <- transform(df, X = as.POSIXct(X))

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