如何在用R绘制时间序列时跳过错过的数据?

时间:2017-11-10 05:52:30

标签: r plot time-series

我有一个文本文件,想要从中读取数据以绘制时间序列,如下所示。

Value   Date    Time
4406    6/13/2016   16:53:23
4422    6/13/2016   17:25:58
4411    6/13/2016   17:31:21
4408    6/13/2016   17:37:00
4404    6/13/2016   17:42:22
-999    6/13/2016   17:47:44
-999    6/13/2016   17:53:12
-999    6/13/2016   18:09:19
-999    6/13/2016   18:20:05
4413    6/13/2016   18:25:30
4418    6/13/2016   18:30:50
4410    6/13/2016   18:36:15
4400    6/13/2016   18:41:46
4397    6/13/2016   18:47:09
4410    6/13/2016   18:58:03
4391    6/13/2016   19:03:29
4449    6/13/2016   19:08:58
4458    6/13/2016   19:14:22
4437    6/13/2016   19:19:49
4406    6/13/2016   19:31:05
4357    6/13/2016   19:36:36
4366    6/13/2016   19:47:53
4363    6/13/2016   19:53:25
4371    6/13/2016   20:04:34
4346    6/13/2016   20:15:50
4347    6/13/2016   20:21:31
4347    6/13/2016   20:27:03
4341    6/13/2016   20:38:13
4330    6/13/2016   20:43:52
4343    6/13/2016   20:55:00
4342    6/13/2016   21:00:31
-999    6/13/2016   21:06:05
4346    6/13/2016   21:11:36
-999    6/13/2016   21:17:14
4347    6/13/2016   21:22:52
4357    6/13/2016   21:28:30
4346    6/13/2016   21:34:05
4350    6/13/2016   21:39:34
4348    6/13/2016   21:45:04
4359    6/13/2016   21:50:38
-999    6/13/2016   21:56:08
4355    6/13/2016   22:01:37
4357    6/13/2016   22:07:12
-999    6/13/2016   22:12:45
4356    6/13/2016   22:18:17
4359    6/13/2016   22:23:50
4358    6/13/2016   22:29:26
4360    6/13/2016   22:34:53
4367    6/13/2016   22:40:20
-999    6/13/2016   22:45:54
4371    6/13/2016   22:51:21
4367    6/13/2016   22:56:49

我有一个用于绘制时间序列的R代码如下。

dm <- read.table(text = Lines, header = TRUE)
dm$Date <- as.Date(dm$Date, "%m/%d/%Y")
plot(Value ~ Date, dm, xaxt = "n", type = "l")
axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7)
title(main= "Time Series", font.main=4, xlab= "Time", ylab= "Water Level")

我想修改代码以在值为-999时停止绘图,然后继续绘图。你能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

将-999转换为NA(R中缺少的值代码)。

library(lubridate)
library(ggplot2)

dm$datetime = mdy_hms(paste(dm$Date, dm$Time))
dm$Value[dm$Value == -999] = NA

使用基本图形进行绘图:

plot(Value ~ datetime, dm, xaxt = "n", type = "l", xlab="", ylab="")
breaks = seq(floor_date(min(dm$datetime), "hour"), max(dm$datetime)+3600,"1 hour")
axis(1, breaks, format(breaks, "%H:%M"), cex.axis = .7)
title(main= "Time Series", font.main=4, xlab= "Time", ylab= "Water Level")

enter image description here

使用ggplot2绘图:

ggplot(dm, aes(datetime, Value)) +
  geom_line() +
  geom_point(size=1) +
  scale_x_datetime(date_breaks="1 hour", date_labels="%H:%M") +
  theme_bw()

enter image description here

您还可以在读取数据时设置NA值,然后您不必再进行转换。例如,如果您使用read.tableread.csv读取数据,则可以将na.strings = "-999"设置为任一函数中的参数。