xts图中的R xlab和ylab

问题描述 投票:0回答:2

绘制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
2个回答
0
投票

我知道这可能与您的想法不完全相同,但是可以完成工作。另外,您可以使用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-05-05创建


0
投票

我们无法说出您做错了什么,因为代码未发布,但是这些对我都有效:

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")
© www.soinside.com 2019 - 2024. All rights reserved.