Search code examples
rplotxts

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..

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.

There must be an easy solution to this. The plot looks as it should besides that issue. I have tried with xts.plot, zoo.plot, xyplot but none seem to do the trick.

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

Solution

  • It is plot.zoo, not 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"
        )))