Can I use polygon to plot xts objects? Or is there any similar function that I can use? I am asking this since I want to fill colours under certain curves (equity curves, say) which are plotted using xts package. Thx!
Asked
Active
Viewed 568 times
1
-
Related question: http://stackoverflow.com/questions/9582033/barplot-for-xts-objects – Vincent Zoonekynd Mar 09 '12 at 07:17
2 Answers
0
Use xts::
addPolygon
, in which coordinates of polygon must be provided as an xts matrix.

Nathan Tuggy
- 2,237
- 27
- 30
- 38

koji
- 1
-
Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. See [here](https://meta.stackexchange.com/a/94027/285661) for instructions how to write *better* "link-based" answers. Thanks! – GhostCat Sep 09 '17 at 05:23
0
You can extract the indices and the values
of an xts object with index
and coredata
.
# Sample data
library(quantmod)
getSymbols("^GSPC")
x <- Vo(GSPC)
n <- length(x)
# Plot
plot(index(x), coredata(x), type="n", las=1, ylim=range(0,x))
polygon(
index(x)[c(1,1:n,n)],
c(0,coredata(x),0),
col="grey"
)
box()

Vincent Zoonekynd
- 31,893
- 5
- 69
- 78
-
Thx again, Vincent! You may notice that this should not be a question any more since a similar question has been solved. But, the situation here is a little bit different. In fact, the code given by you do not display the scale. I've tried the following code, in which case scale can be displayed correctly, but the side effect is that polygon seems not working: library(quantmod) getSymbols("^GSPC") x <- Vo(GSPC) n <- length(x) # Plot plot(as.xts(x), type="l", las=1, ylim=range(0,x)) polygon( index(x)[c(1,1:n,n)], c(0,coredata(x),0), col="grey" ) box() – billlee1231 Mar 09 '12 at 08:16