Remove y-axis label from plot of an xts object

六月ゝ 毕业季﹏ 提交于 2019-12-01 21:06:12

问题


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

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

Which yields the following plot:

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

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


回答1:


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.




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




回答3:


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



来源:https://stackoverflow.com/questions/6165239/remove-y-axis-label-from-plot-of-an-xts-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!