【问题标题】:R xlab and ylab in xts plotxts图中的R xlab和ylab
【发布时间】:2020-08-20 07:05:55
【问题描述】:

绘制 xts 应该很容易,但我似乎无法让 xlab 和 ylab 工作..

ylab="Price"
xlab="Date"


plot(x = basket[, 1], lty="dotted", xlab = xlab, ylab = ylab, col = "mediumblue", lwd = 2) 
title("Cumulative")

这将绘制图形,但在 x 轴和 y 轴上没有任何标签。

必须有一个简单的解决方案。除了那个问题,情节看起来应该是这样。我尝试过 xts.plot、zoo.plot、xyplot,但似乎都没有成功。

数据样本

structure(c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073, 
0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889, 
1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567, 
1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013, 
1.04571606282071), class = c("xts", "zoo"), index = structure(c(946944000, 
947030400, 947116800, 947203200, 947462400), tzone = "UTC", tclass = "Date"), .Dim = 5:4, .Dimnames = list(
    NULL, c("new.close", "new.close.1", "new.close.2", "new.close.3"
    )))

【问题讨论】:

    标签: r plot xts


    【解决方案1】:

    它是plot.zoo,而不是zoo.plot。这些都对我有用:

    library(xts)
    
    plot(as.zoo(basket[, 1]), xlab = "X", ylab = "Y")
    
    plot.zoo(basket[, 1], xlab = "X", ylab = "Y")
    
    library(lattice)
    xyplot(basket[, 1], xlab = "X", ylab = "Y")
    
    library(ggplot2)
    autoplot(basket[, 1]) + xlab("X") + ylab("Y")
    

    注意

    basket <-
    structure(c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073, 
    0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889, 
    1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567, 
    1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013, 
    1.04571606282071), class = c("xts", "zoo"), index = structure(c(946944000, 
    947030400, 947116800, 947203200, 947462400), tzone = "UTC", tclass = "Date"), .Dim = 5:4, .Dimnames = list(
        NULL, c("new.close", "new.close.1", "new.close.2", "new.close.3"
        )))
    

    【讨论】:

    • 我的 xts 绘图设置显然有问题。他们都没有在 X 和 Y 轴上绘制任何东西。我确实尝试删除了情节被接管的时间间隔的标题,这一定搞砸了。 ggplot2 确实有效,所以我想我将不得不使用它。谢谢!
    • 使用R --vanilla启动一个新的R实例以确保没有任何内容被拉入。然后从答案末尾的注释中复制并粘贴basket ,然后复制并粘贴代码在答案中。
    【解决方案2】:

    我知道这可能与您的想法不完全一致,但它可以完成工作。另外,您可以使用 ggplot2 比标准绘图系统更好。此外,我正在使用 ggfortify,它扩展了ggplot() 处理时间序列的能力。

    library(xts)
    library(zoo)
    library(ggfortify)   
    library(ggplot2)
    
    myts = structure( 
      c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073, 
        0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889, 
        1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567, 
        1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013, 
        1.04571606282071), 
      class = c("xts", "zoo"), 
      index = structure(c(946944000, 947030400, 
                          947116800, 947203200, 
                          947462400), 
                        tzone = "UTC", 
                        tclass = "Date"), 
      .Dim = 5:4, .Dimnames = list(NULL, 
                                   c("new.close", 
                                     "new.close.1", 
                                     "new.close.2", 
                                     "new.close.3" ) ) 
    )
    
    autoplot( myts[ , 1], xlab = "Date", ylab = "Price" )
    

    reprex package (v0.3.0) 于 2020 年 5 月 5 日创建

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 2019-07-06
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2020-08-19
      相关资源
      最近更新 更多