[英]connect points in a time series with NA fields in R
This is the data frame i am working with:这是我正在使用的数据框:
Month HSI GSI K
Dec-12 1.703 0.516 0.315
Jan-13 1.841 0.441 0.316
Feb-13 NA NA NA
Mar-13 NA NA NA
Apr-13 NA NA NA
May-13 3.365 0.627 0.324
Jun-13 NA NA NA
Jul-13 NA NA NA
Aug-13 4.097 0.456 0.317
Sep-13 NA NA NA
Oct-13 2.582 0.977 0.336
Nov-13 3.728 1.178 0.352
Dec-13 2.211 3.937 0.352
Jan-14 1.617 1.163 0.336
I am trying to plot HSI, GSI and K on the same graph.我试图在同一张图上绘制 HSI、GSI 和 K。 I have no problem with that.对于那件事我没有任何疑问。 However, my problem is that my curves are discontinuous due to the NA
fields, and i am only getting the year on the x axis.但是,我的问题是由于NA
字段,我的曲线不连续,而且我只在 x 轴上得到年份。 This is what u an using:这是你使用的:
library(zoo) #to plot multiple lines in the graph
library(timeDate) #to create a time series by month
tS = timeSequence(from = "2012-12-01", to = "2014-01-01", by = "month")
plot(tS,HSI, type="l",ann="False",ylim=c(1,5), pch=22, lty=1,lwd=2, col="red")
lines(tS,GSI,type="l",pch=22,lty=1,lwd=2, col="green") #to add other lines
lines(tS,K, type="l",pch=22,lty=1,lwd=2, col="blue")
Any help please?请问有什么帮助吗? Thank you谢谢
You may convert your data frame to zoo
object and use the plot.zoo
facilities:您可以将数据框转换为zoo
对象并使用plot.zoo
设施:
library(zoo)
z <- zoo(df[ , c("HSI", "GSI", "K")], order.by = as.yearmon(df$Month, "%b-%y"))
plot(na.omit(z), plot.type = "single", col = c("red", "green", "blue"))
plot(na.omit(z), plot.type = "single", col = c("red", "green", "blue"), xaxt = "n")
axis(side = 1, at = index(z), labels = format(index(z), "%b-%y"))
You may also try ggplot
alternatives:您也可以尝试ggplot
替代方案:
library(scales)
library(ggplot2)
z2 <- fortify(z, melt = TRUE)
ggplot(data = na.omit(z2), aes(x = Index, y = Value, colour = Series)) +
geom_line() +
scale_x_yearmon(breaks = z2$Index, format = "%b %Y") +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
Another ggplot
possibility:另一种ggplot
可能性:
# convert year-month dates to as.POSIXct
df$Month <- as.POSIXct(as.Date(paste0(df$Month, "-01"), "%b-%y-%d"))
# reshape data from wide to long
library(reshape2)
df2 <- melt(df, id.var = "Month")
ggplot(data = na.omit(df2), aes(x = Month, y = value, colour = variable)) +
geom_line()
When you have dates in as.POSIXct
format you can easily format labels using scale_x_datetime
: breaks = date_breaks
and labels = date_format
, eg当您有as.POSIXct
格式的日期时,您可以使用scale_x_datetime
轻松格式化标签: breaks = date_breaks
和labels = date_format
,例如
ggplot(data = na.omit(df2), aes(x = Month, y = value, colour = variable)) +
geom_line() +
scale_x_datetime(breaks = date_breaks("2 months"),
labels = date_format("%Y-%m"))
You can use complete.cases
as follows.您可以使用complete.cases
如下。 Let t
be your data.frame:让t
成为您的 data.frame:
x <- complete.cases(t)
plot(tS[x],t$HSI[x], type="l",ann="False",ylim=c(0,5), pch=22, lty=1,lwd=2, col="red")
lines(tS[x],t$GSI[x],type="l",pch=22,lty=1,lwd=2, col="green") #to add other lines
lines(tS[x],t$K[x], type="l",pch=22,lty=1,lwd=2, col="blue")
Note that I changed the ylim
in the call to plot
because the blue line was outside the plot limits.请注意,我在调用plot
更改了ylim
,因为蓝线超出了绘图限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.