绘制XTS对象时的更改 [英] Changes in plotting an XTS object

查看:118
本文介绍了绘制XTS对象时的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了下面的图表,该图表是使用xts对象创建的.

I had produced the following chart, which is created using an xts object.

我使用的代码很简单

   plot(graphTS1$CCLL, type = "l", las = 2, ylab = "(c)\nCC for Investors", xlab = "", main = "Clustering Coefficient at the Investor Level")

我更新了我的R Studio软件包和R版本,然后通过运行相同的代码得到以下图表.

I updated my R Studio Package and R version and then by running the same code I got the following chart.

很明显,两者并不相同.人们是否可以帮助我移除第二个Y轴-我不需要那个,并移除2007-03-01/2010-12-01的自动前进方向.许多尝试都失败了.我确实使用了zoo.plot,但是删除了网格线,能力和季度标记.

Obviously the two are not the same. Can people help me remove the second Y axis - I do not need that, and remove the automatic heading of 2007-03-01 / 2010-12-01. Numerous attempts of this have failed. I did use zoo.plot but the gridlines and ability and the quarterly marks were removed.

我的R版本是3.4.0(2017-04-21),具有以下平台"x86_64-apple-darwin15.6.0".预先感谢.

My R version is 3.4.0 (2017-04-21) with the following platform "x86_64-apple-darwin15.6.0". Thanks in advance.

推荐答案

您要删除右手轴. plot.xts(..., yaxis.right = TRUE, ...)中有一个参数.所以

You want to remove the right hand axis. There is an argument in plot.xts(..., yaxis.right = TRUE, ...). So

library('xts')
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric

data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')

plot(sample.xts, yaxis.right = FALSE)

做您想要的事.

我试图解决第二个问题,删除了右上角的标签.检查plot.xts()的源代码可以发现标签已硬编码到主标题中.即使设置main = ''也不会将其删除.您可以通过编辑plot.xts()并将其复制到新功能中来解决此问题.

I took a stab at solving the second question, removing the label at the top right hand side. Examining the source code for plot.xts() reveals that label is hardcoded into the main title. Even setting main = '' isn't going to remove it. You can work around it by editing plot.xts() and copying it to a new function.

plotxts <- fix("plot.xts")
# In the editor that opens, replace the lines below:
###    text.exp <- c(expression(text(xlim[1], 0.5, main, font = 2, 
###        col = theme$labels, offset = 0, cex = 1.1, pos = 4)), 
###        expression(text(xlim[2], 0.5, paste(start(xdata[xsubset]), 
###            end(xdata[xsubset]), sep = " / "), col = theme$labels, 
###            adj = c(0, 0), pos = 2)))
###    cs$add(text.exp, env = cs$Env, expr = TRUE)
# with these lines:
###    text.exp <- expression(text(xlim[1], 0.5, main, font = 2,
###        col = theme$labels, offset = 0, cex = 1.1, pos = 4))

# Finally, you need to ensure your copy's environment is the xts namespace:
environment(plotxts) <- asNamespace("xts")

plotxts(sample.xts, yaxis.right = FALSE, main = "Main Title")

第二个,也许 更简单的选择是使用其他绘图功能并对其进行修改以生成所需的网格线等.我会开始 使用plot.zoo(),因为它已经很好地处理了时间序列.

The second, and perhaps simpler option is to use a different plot function and modify it to produce the gridlines etc that you want. I will start with plot.zoo() because it is already handling time series nicely.

zoo::plot.zoo(sample.xts, screens = 1, xlab="", las=2, main="Main Title")
grid() # add the grid

至少可以将网格放置在那里.我无法测试如果没有正确频率的数据,它是否会以相同的方式处理x轴标签.

That at least gets the grid on there. I can't test if it will handle the x axis labels the same way without data at the right frequency.

这篇关于绘制XTS对象时的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆