【问题标题】:Plotting two xts objects绘制两个 xts 对象
【发布时间】:2012-11-22 15:34:14
【问题描述】:

我正在使用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))

产生:

【问题讨论】:

  • 另一种方法:plot(merge(a,b), yax.loc='flip')
  • @Julian 为什么你不给出一个可重复的例子? a和b?
  • 这是一个可能有用的方法。 stackoverflow.com/questions/5479822/…
  • @agstudy 好电话,我更新了我的问题!
  • @bill_080 我看不出这段代码是如何调整 yaxis 范围的。你说的是这个电话吗? yaxisdat<-seq(pardat$yaxp[1],pardat$yaxp[2],(pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3]我无法解析代码。

标签: r xts


【解决方案1】:

抱歉,拖了这么久。我试图弄清楚为什么我的图表从 2012 年 12 月 4 日开始,到 2012 年 12 月 13 日结束,而你的图表从 2012 年 12 月 5 日开始,到 2012 年 12 月 14 日结束。你能检查一下以确保ab你上面发布的 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)

【讨论】:

  • xtsExtra 有一个新的 plot.xts 方法,它是作为 Google Summer of Code 项目的一部分而创建的。见blog.fosstrading.com/2012/08/a-new-plot-xts.html
  • @GSee 显然,xtsExtra 不能在 R-2.14 下运行。我必须更新我的 R 版本。
  • 它在xtsExtra/R/plot.R 中出现了一次paste0(在R-2.15 中引入)。用 paste(..., sep="") 替换它可能足以使用 R-2.14
【解决方案2】:

我玩一些参数

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

【讨论】:

    【解决方案3】:

    由于 y 轴的格式不佳,我放弃了使用 xts 绘图。 和这个问题中提到的完全一样的问题。

    autoplot.zoo 是一个不错的选择。

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

    【讨论】:

      猜你喜欢
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多