4

Here is the code generating a plot of an xts object:

require("quantmod")
getSymbols("SPY")
plot(Cl(SPY))

Which yields the following plot:

graph

Can you remove the y-axis values (the prices) from a plot of an xts object?

Hint: passing yaxt='n' doesn't work.

Geoff Dalgas
  • 5,948
  • 6
  • 40
  • 58
Milktrader
  • 8,660
  • 12
  • 49
  • 68

3 Answers3

6

Removing the y-axis is easy, but it also removes the x-axis. A couple options:

1) Easy -- use plot.zoo:

plot.zoo(Cl(SPY), yaxt="n", ylab="")

2) Harder-ish -- take pieces from plot.xts:

plot(Cl(SPY), axes=FALSE)
axis(1, at=xy.coords(.index(SPY), SPY[, 1])$x[axTicksByTime(SPY)],
  label=names(axTicksByTime(SPY)), mgp = c(3, 2, 0))

3) Customize-ish -- modify plot.xts so axes= accepts a vector of axes to plot and/or TRUE/FALSE.

Joshua Ulrich
  • 168,168
  • 29
  • 327
  • 408
1

Adding to Joshua's answer, to modify plot.xts(), all you need to do is alter the following section:

    if (axes) {
      if (minor.ticks) 
        axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB")
      axis(1, at = xycoords$x[ep], labels = names(ep), las = 1,lwd = 1, mgp = c(3, 2, 0))
    #This is the line to change:
    if (plotYaxis) axis(2)
    }

and obviously you add the parameter plotYaxis=TRUE to the function definition.

joran
  • 163,977
  • 32
  • 423
  • 453
0

You can also try specifying that the x and y as labels are empty, or contain no values/characters. Try using the term xlab="" in your plot command: e.g. plot(beers,ranking,xlab="",ylab="")

By not including anything between the quotation marks, R doesn't plot anything. Using this command, you can also specific the labels, so to make the label for the x axis 'beer', use the term xlab="beer".

plannapus
  • 18,099
  • 4
  • 72
  • 92
AndyC
  • 1