r Error: Don't know how to add o to a plot

SteveM49 picture SteveM49 · Apr 1, 2017 · Viewed 17.5k times · Source

I am trying to add arrow heads to a line created by geom_line() in ggplot. Example:

library(grid)
library(ggplot2)
df <- data.frame(x=(1:2),y=(2:1))
ggplot(df,aes(x,y)) +
    geom_line() +
    arrow()

The error I get is "Error: Don't know how to add o to a plot".
I found a variety of posts with this error, but they had a complex scenario and the answers called for making what looked like subtle changes in the code.

If instead I run:

ggplot(data=cinterval,aes(x=x,y=y))+
    geom_line()

I get the expected line. When I run the expanded code, with a more elaborate plot, I get the error "Error in as.vector(y) : attempt to apply non-function"

Answer

Kay picture Kay · Apr 1, 2017

Do this instead

 library(grid)
library(ggplot2)
df <- data.frame(x=(1:2),y=(2:1))
ggplot(df,aes(x,y)) +
 geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type ="closed"))