1

I want to try ggplot with some time series data (stocks) and would like to pass x and y axis values to the ggplot function.

Here's my sample code:

require(quantmod)
getSymbols("AAPL")
AAPL.DF<-as.data.frame(AAPL)

Interesting is here that we have only 1729x6 observations, even though the data column is there as well (so it should be actually 7 columns...). If I want to see all column names (in order to reference it for ggplot) with colnames(AAPL.DF) I only get the column names starting with AAPL.Open but the data column isn't mentioned. Why is that? And how can I reference it then for ggplot?!

MichiZH
  • 5,587
  • 12
  • 41
  • 81
  • 1
    I'm sure there is a *proper* way to do this and the structure of the `xts` object is like this for a very good reason, but you can add a new column to your data.frame with the dates, thus: `AAPL.DF$Date <- attr( AAPL.DF , "row.names" ) ` – Simon O'Hanlon Nov 13 '13 at 09:58
  • 1
    Why do you need to use `ggplot2`? `xtsExtra`'s `plot.xts` and `quantmod`'s `chart_Series` are very good at plotting `xts` objects. – CHP Nov 13 '13 at 10:39

2 Answers2

4

After searching once more on stackoverflow I found another solution:

require(quantmod)
getSymbols("AAPL")
AAPL.DF<-data.frame(Date=index(AAPL),coredata(AAPL))

That does the trick:-)

MichiZH
  • 5,587
  • 12
  • 41
  • 81
3

You don't even need to make the data.frame...autoplot might be able to help

autoplot(AAPL$AAPL.Adjusted)

should do the trick...(assuming you wanted the Adjusted price...)

or if you want everything...

autoplot(AAPL)+facet_free()
h.l.m
  • 13,015
  • 22
  • 82
  • 169