簡體   English   中英

R xts 中的 xlab 和 ylab plot

[英]R xlab and ylab in xts plot

繪制 xts 應該很容易,但我似乎無法讓 xlab 和 ylab 工作..

ylab="Price"
xlab="Date"


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

這將 plot 圖形,但在 x 軸和 y 軸上沒有任何標簽。

必須有一個簡單的解決方案。 除了那個問題,plot 看起來應該是這樣。 我試過 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"
    )))

我知道這可能不是您的想法,但它可以完成工作。 此外,您還可以使用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 日創建

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM