Skip to content
Prev 14651 / 15274 Next

Error plotting a zoo object

Hi Baki,

Please follow the posting guide and use plain text.  Your HTML message was
mangled.

Here's a simple example that reproduces the behavior:
library(quantmod)
library(ggplot2)
bist100 <- getSymbols("XU100.IS", from="2011-01-01")
bist100 <- Cl(XU100.IS)
bist100na <- na.approx(bist100)
ggplot(bist100na, aes(x = date, y = XU100.IS.Close)) + geom_line()

You get an error because "date" is a function in 'base'.

You can either use 'autoplot()`:
autoplot(bist100na)

Or you can fortify the object yourself, but then you have to name the `x`
and `y` columns correctly.  By default, the xts/zoo index is put in a
column named "Index".  So the ggplot() call would look like:
ggplot(fortify(bist100na), aes(x = Index, y = XU100.IS.Close)) + geom_line()

It is also very easy to use xts or zoo plotting via the plot() function:
plot(bist100na)

Hope that helps.

Best,
Josh


On Thu, Nov 15, 2018 at 5:34 PM Baki UNAL via R-SIG-Finance <
r-sig-finance at r-project.org> wrote: