【问题标题】:Changes in plotting an XTS object绘制 XTS 对象的变化
【发布时间】:2018-10-07 15:08:15
【问题描述】:

我制作了以下图表,它是使用 xts 对象创建的。

我使用的代码很简单

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

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

显然两者并不相同。人们可以帮我删除第二个 Y 轴吗?我不需要,并删除 2007-03-01 / 2010-12-01 的自动标题。许多这样的尝试都失败了。我确实使用了 zoo.plot,但网格线和能力以及季度标记被删除了。

我的 R 版本是 3.4.0 (2017-04-21),平台为“x86_64-apple-darwin15.6.0”。提前致谢。

【问题讨论】:

    标签: r plot xts zoo


    【解决方案1】:

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

    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() 并将其复制到新函数来解决此问题。

    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() 因为它已经很好地处理了时间序列。

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

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

    【讨论】:

    • 感谢您为我回答这个问题,并为迟来的确认感到抱歉。在短期内,我会选择选项 2,但会考虑使用第一个选项。
    • fix()...这改变了一切
    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 2017-03-27
    • 2019-06-15
    • 1970-01-01
    • 2020-01-20
    • 2021-07-14
    • 2016-12-13
    • 2015-12-04
    相关资源
    最近更新 更多