简体   繁体   中英

Time series double y axis plot with x axis in “year”-“month” in R

I have the following code for a double y axis plot in r. Everything works fine, but I want to change the x axis from every 18 months to every 12 months. I have tried every "axis(side=1, at=....)" I could think of. Any random number between 0:1.5 will work for "Data$Monthly_Gen" and 0:100 for Data$Ave_GenXXXX" for reproducing. Thanks.

Data2 <- ts(Data$Monthly_Gen, start=c(2005,1),end=c(2012,12),frequency=12)   
Data2B <- ts(Data$Ave_Gen_MonthSOCO, start=c(2005,1),end=c(2012,12),frequency=12)  
Data2C <- ts(Data$Ave_Gen_MonthTVA, start=c(2005,1),end=c(2012,12),frequency=12) 
Data2D<-ts(Data$Monthly_Gen_Othersx10,start=c(2005,1),end=c(2012,12),frequency=12)
Data2E <- ts(Data$Monthly_Gen_Othersx9, start=c(2005,1),end=c(2012,12),frequency=12)



par(mar=c(4, 4, 2, 4) + 0.1)
plot(as.xts(Data2), major.format = "%Y-%m",ylab="",las=1,ylim=c(0,2))
lines(as.xts(Data2D), major.format = "%Y-%m", xlab="", ylab="",  
type="l", col="black",main="",lty="dotted" ,lwd=2)  
lines(as.xts(Data2E), major.format = "%Y-%m", xlab="", ylab="",  
type="l", col="black",main="",lty="longdash" ,lwd=2)  
mtext("Generation",side=2,line=3)  
box()
par(new=TRUE)  
plot(as.xts(Data2B), major.format = "%Y-%m", xlab="", ylab="",  
axes=FALSE, type="l", col="#E69F00",main="",lwd=2,ylim=c(0,130))  
mtext("Monthly Average Lambda",side=4,col="black",line=2.5)  
axis(4, col="black",col.axis="black",las=1)
lines(as.xts(Data2C), major.format = "%Y-%m", xlab="", ylab="",  
type="l", col="#56B4E9",main="",lwd=2.5)
mtext("Date",side=1,col="black",line=3)
legend("topright",legend=c("Total Generation","X1 Generation","X2 Generation","Area Lambda","X2 Area Lambda"),text.col=c("black","black","black","#E69F00","#56B4E9"),col=c("black","black","black","#E69F00","#56B4E9"),cex=.75,lty=c("solid","longdash","dotted","solid","solid"))`

This is easy to do with zoo . In my opinion, zoo is more powerful than ts .

#Create zoo series
Data2 <- zooreg(runif(96,0,1.5), start = as.yearmon(2005),end = as.yearmon(2012), freq = 12)
Data2B <- zooreg(runif(96,0,100), start = as.yearmon(2005),end = as.yearmon(2012), freq = 12)

#Create 12-month sequence for axis
twelve <-seq(1,length(Data2),12)

plot(Data2,ylab="",las=1,ylim=c(0,2),xaxt = "n")
#add x-axis at 12 months
axis(1,at=index(Data2)[twelve],labels=format(index(Data2)[twelve],"%Y-%m"))
par(new=TRUE)
plot(Data2B,xlab="",ylab="",las=1,ylim=c(0,130),xaxt = "n",yaxt = "n",col="blue")
#add yy axis
axis(4, col="black",col.axis="black",las=1)

在此处输入图片说明

Here's a solution using only the base plot() functions. I think what you really want is seq.Date() for something like this

set.seed(123)
nn <- 7*12  # months
x1 <- ts(rnorm(nn),start=c(2005,1),freq=12)
x2 <- ts(rpois(nn,20),start=c(2005,1),freq=12)
tt <- seq.Date(from=as.Date("2005-01-01"),by="month",length.out=nn)
plot(tt, x1, col="blue", type="l", xaxt="n", xlab="", ylab="")
par(new=TRUE)
plot(tt, x2, col="red", type="l", xaxt="n", yaxt="n", xlab="", ylab="")
axis(4)
dates <- seq(from=as.Date("2005-01-01"), to=as.Date("2012-12-01"), by="18 month")
axis(1, at=dates, labels = format(dates, "%Y-%m"))

在此处输入图片说明

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