Changes in plotting an XTS object

孤街浪徒 提交于 2019-11-28 06:01:27

问题


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

The code I used was simply

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

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

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.

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


回答1:


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)

does what you want.

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")

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

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.



来源:https://stackoverflow.com/questions/50051076/changes-in-plotting-an-xts-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!