Changes in plotting an XTS object

前端 未结 1 733
小鲜肉
小鲜肉 2020-12-20 02:06

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

The code I used was simply

   plot(graphTS1$CCLL, type = \"l\", la         


        
相关标签:
1条回答
  • 2020-12-20 02:53

    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.

    0 讨论(0)
提交回复
热议问题