2012-12-04 187 views
6

我使用xtsExtra繪製兩個xts對象。繪製兩個xts對象

考慮以下調用plot.xts:

plot.xts(merge(a,b),screens=c(1,2)) 

其用於繪製XTS對象A和B在兩個單獨的面板。

如何控制y軸的間距?具體來說,我遇到了y軸標籤太接近甚至重疊的問題。

理想情況下,我想指定兩個y軸標籤之間要保留的最小填充。任何幫助表示讚賞!

編輯:可再現例如:

#install if needed 
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org") 
library(xtsExtra) 

ab=structure(c(-1, 0.579760106421202, -0.693649703427259, 0.0960078627769613, 
0.829770469089809, -0.804276208608663, 0.72574639798749, 0.977165659135716, 
-0.880178529686181, -0.662078620277974, -1, 2.35268982675599, 
-0.673979231663719, 0.0673890875594205, 1.46584597734824, 0.38403707067242, 
-1.53638088345349, 0.868743976582955, -1.8394614923913, 0.246736581314485 
), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("a", "b")), index = structure(c(1354683600, 
1354770000, 1354856400, 1354942800, 1355029200, 1355115600, 1355202000, 
1355288400, 1355374800, 1355461200), tzone = "", tclass = "Date"), class = c("xts", 
"zoo"), .indexCLASS = "Date", .indexTZ = "", tclass = "Date", tzone = "") 

plot.xts(ab,screens=c(1,2)) 

其產生:

y-axis labels too close

+1

的另一種方法:'圖(合併(A,B),yax.loc = '倒裝')' – GSee

+2

@Julian你爲什麼不給reprodicible例子嗎? a和b? – agstudy

+0

這是一個可能有用的方法。 http://stackoverflow.com/questions/5479822/plotting-4-curves-in-a-single-plot-with-3-y-axes-in-r/5480489#5480489 –

回答

3

對不起,該了這麼長時間。我試圖找出爲什麼我的圖表從2012年12月4日開始,到2012年12月13日結束,當時您的圖表始於2012年12月5日,並於2012年12月14日結束。您可以檢查以確保您發佈的ab以上你用來繪製你的圖的是相同的ab

此外,我用庫xts而不是xtsExtra。有沒有理由使用xtsExtra

下面的代碼:

library(xts) 

ab=structure(c(-1, 0.579760106421202, -0.693649703427259, 0.0960078627769613, 
      0.829770469089809, -0.804276208608663, 0.72574639798749, 0.977165659135716, 
      -0.880178529686181, -0.662078620277974, -1, 2.35268982675599, 
      -0.673979231663719, 0.0673890875594205, 1.46584597734824, 0.38403707067242, 
      -1.53638088345349, 0.868743976582955, -1.8394614923913, 0.246736581314485), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("a", "b")), index = structure(c(1354683600, 
      1354770000, 1354856400, 1354942800, 1355029200, 1355115600, 1355202000, 
      1355288400, 1355374800, 1355461200), tzone = "", tclass = "Date"), class = c("xts", 
      "zoo"), .indexCLASS = "Date", .indexTZ = "", tclass = "Date", tzone = "") 

#Set up the plot area so that multiple graphs can be crammed together 
#In the "par()" statement below, the "mar=c(0.3, 0, 0, 0)" part is used to change 
#the spacing between the graphs. "mar=c(0, 0, 0, 0)" is zero spacing. 
par(pty="m", plt=c(0.1, 0.9, 0.1, 0.9), omd=c(0.1, 0.9, 0.2, 0.9), mar=c(0.3, 0, 0, 0)) 

#Set the area up for 2 plots 
par(mfrow = c(2, 1)) 

#Build the x values so that plot() can be used, allowing more control over the format 
xval <- index(ab) 

#Plot the top graph with nothing in it ========================= 
plot(x=xval, y=ab$a, type="n", xaxt="n", yaxt="n", main="", xlab="", ylab="") 
mtext(text="ab", side=3, font=2, line=0.5, cex=1.5) 

#Store the x-axis data of the top plot so it can be used on the other graphs 
pardat <- par() 

#Layout the x axis tick marks 
xaxisdat <- index(ab) 

#If you want the default plot tick mark locations, un-comment the following calculation 
#xaxisdat <- seq(pardat$xaxp[1], pardat$xaxp[2], (pardat$xaxp[2]-pardat$xaxp[1])/pardat$xaxp[3]) 

#Get the y-axis data and add the lines and label 
yaxisdat <- seq(pardat$yaxp[1], pardat$yaxp[2], (pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3]) 
axis(side=2, at=yaxisdat, las=2, padj=0.5, cex.axis=0.8, hadj=0.5, tcl=-0.3) 
abline(v=xaxisdat, col="lightgray") 
abline(h=yaxisdat, col="lightgray") 
mtext(text="ab$a", side=2, line=2.3) 
lines(x=xval, y=ab$a, col="red") 
box() #Draw an outline to make sure that any overlapping abline(v)'s or abline(h)'s are covered 

#Plot the 2nd graph with nothing in it ================================ 
plot(x=xval, y=ab$b, type="n", xaxt="n", yaxt="n", main="", xlab="", ylab="") 

#Get the y-axis data and add the lines and label 
pardat <- par() 
yaxisdat <- seq(pardat$yaxp[1], pardat$yaxp[2], (pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3]) 
axis(side=2, at=yaxisdat, las=2, padj=0.5, cex.axis=0.8, hadj=0.5, tcl=-0.3) 
abline(v=xaxisdat, col="lightgray") 
abline(h=yaxisdat, col="lightgray") 
mtext(text="ab$b", side=2, line=2.3) 
lines(x=xval, y=ab$b, col="blue") 
box() #Draw an outline to make sure that any overlapping abline(v)'s or abline(h)'s are covered 

#Plot the X axis ================================================= 
axis(side=1, label=format(as.Date(xaxisdat), "%b %d\n%Y\n") , at=xaxisdat, padj=0.4, cex.axis=0.8, hadj=0.5, tcl=-0.3) 
mtext(text="Date", side=1, line=2.5) 

enter image description here

+0

'xtsExtra'有一個新的'plot.xts'方法,它是作爲Google Summer of Code項目的一部分創建的。見http://blog.fosstrading.com/2012/08/a-new-plot-xts.html – GSee

+0

@GSee顯然,'xtsExtra'不能在R-2.14下運行。我將不得不更新我的R版本。 –

+0

它在'xtsExtra/R/plot.R'中有'paste0'(在R-2.15中引入)。用'paste(...,sep =「」)替換它可能足以使用R-2.14 – GSee

1

我的一些參數

plot.xts(ab, bty = "n", las = 1,  cex.axis = 0.5) 

enter image description here

1

打我放棄了,因爲在窮人格式的XTS繪圖y軸。 在這個問題中提到的問題完全一樣。

autoplot.zoo一直是一個不錯的選擇。

library(ggplot2) 
autoplot.zoo(ab) + theme(panel.background = element_blank(), 
         panel.grid.major = element_blank(), 
         panel.grid.minor = element_blank()) + 
         theme_bw() 

enter image description here