r - 绘制 XTS 对象时的变化

标签 r plot xts zoo

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

enter image description here

我使用的代码很简单

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

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

enter image description here

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

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

最佳答案

您想删除右手轴。 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 轴标签。

关于r - 绘制 XTS 对象时的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051076/

相关文章:

r - 在 R 中使用 cowplot 对齐 image() 图

renderGvis 在 rmarkdown 中不起作用

r - 动态创建嵌套的 s4 类插槽名称并在 R 中分配值

python - 如何在网络风格的 Plotly 图中设置单独的线宽(Python 3.6 | plot.ly)?

r - ggplot 堆积面积

替换多列的 diff()

r - 使用rollapply函数使用R进行VaR计算

r - 邻近矩阵 - 随机森林,R

r - 连接/合并时间序列(在 R 中)

r - 在 R 中绘制多元多项式