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
  • 6,116
  • 6
  • 42
  • 58
Milktrader
  • 9,278
  • 12
  • 51
  • 69

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
  • 173,410
  • 32
  • 338
  • 418
2

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
  • 169,992
  • 32
  • 429
  • 468
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,529
  • 4
  • 72
  • 94
AndyC
  • 1