简体   繁体   English

R xts 中的 xlab 和 ylab plot

[英]R xlab and ylab in xts plot

Plotting an xts should be really easy, but I can't seem to get the xlab, and ylab to work..绘制 xts 应该很容易,但我似乎无法让 xlab 和 ylab 工作..

ylab="Price"
xlab="Date"


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

This will plot the figure, but without any labels on the x- and y-axis.这将 plot 图形,但在 x 轴和 y 轴上没有任何标签。

There must be an easy solution to this.必须有一个简单的解决方案。 The plot looks as it should besides that issue.除了那个问题,plot 看起来应该是这样。 I have tried with xts.plot, zoo.plot, xyplot but none seem to do the trick.我试过 xts.plot, zoo.plot, xyplot 但似乎没有一个能解决问题。

Data sample数据样本

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

I know this might not be exactly what you had in mind, but it gets the job done.我知道这可能不是您的想法,但它可以完成工作。 Plus, you get to work with ggplot2 which way better than the standard plotting system.此外,您还可以使用ggplot2 ,这比标准绘图系统要好得多。 In addition, I am using ggfortify which extends ggplot() capabilities to handle time series.此外,我正在使用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" )

Created on 2020-05-05 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2020 年 5 月 5 日创建

It is plot.zoo , not zoo.plot .它是plot.zoo ,而不是zoo.plot These all work for me:这些都对我有用:

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

Note笔记

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