3
我使用此代碼如何在ggplot2中繪製xts?
library(xts)
library(zoo)
plot.new()
par(mai=c(2, 2, 1, 1))
plot(nats[,2], ylim=c(0, 2.5),
xlab = "", ylab="", main="", major.ticks="months",
major.format="%d-%b-%Y", minor.ticks=FALSE, las=1, cex.axis=0.8)
points(nats[rans,2], col="darkgrey", pch=16, cex=0.5)
points(M1[rans], col="red", pch=16, cex=0.5)
points(M2[rans], col="blue", pch=16, cex=0.5)
points(M3[rans], col="green", pch=16, cex=0.5)
legend(x="topright",
legend=c("a", "Actual value", "M1","M2","M3"),
col=c("Black", "Grey", "Red","Blue","Green"),
lwd=1, lty=c(1,NA,NA,NA, NA),
pch=c(NA,16,16,16,16), merge=FALSE, cex=0.6)
DATA該地塊
set.seed(123)
date <- as.Date(seq(as.Date("2003-01-01"),
as.Date("2003-05-31"), by = 1), format="%Y-%m-%d")
a <- runif(151, 0.005, 2.3)
df <- data.frame(date, a)
#select 30 random samples
rans<-sample(length(df$a), 30)
df$a.rm <- df$a
#set the values for the selected rows as NA
df[rans, 3]<-NA
#reorder columns
df <- subset(df, select=c(1,3,2))
# test 3 methods for filling NA
nats<-xts(df[,-1], as.POSIXct(df$date))
M1<-na.locf.default(nats[,1])
M2<-na.approx(nats[,1])
M3<-na.spline(nats[,1])
任何建議如何使用ggplot2
繪製應該理解的?
掃帚::整潔()是防禦一個更好的替代() –
@YunChing 你是對的情節。謝謝 – aelwan