同时绘制两个xts对象

6
我将使用xtsExtra来绘制两个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轴标签过于接近


1
另一种方法:plot(merge(a,b), yax.loc='flip') - GSee
2
@Julian,你为什么不提供一个可复现的示例?a和b是什么? - agstudy
以下是一种可能有用的方法。https://dev59.com/zG035IYBdhLWcg3wVuh1#5480489 - bill_080
@agstudy,好的,谢谢提醒!我已经更新了我的问题。 - Julian
@Julian,这种方法的想法是,您可以完全控制图形的格式。我会使用您的示例添加一个答案。 - bill_080
显示剩余2条评论
3个回答

3
抱歉让你等这么久。我一直在尝试弄清为什么我的图表从2012年12月04日开始,到2012年12月13日结束,而你的图表从2012年12月05日开始,到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


xtsExtra有一个新的plot.xts方法,这是Google Summer of Code项目的一部分创建的。请参阅http://blog.fosstrading.com/2012/08/a-new-plot-xts.html - GSee
@GSee 显然,xtsExtra不支持R-2.14。我需要更新我的R版本。 - bill_080
它在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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接