Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

r - how to draw arrow in ggplot2 with annotation

I am creating a ggplot chart where I need to put some annotation. I can do that but the text is not really pointing exactly the date that I like annotate. Is there a way to draw an arrow to axix where I like create the annotation.

I have this:

   ggplot(df, aes(Date, CPU)) + geom_point() + 
       annotate("text", x = as.POSIXct(c("2013-03-11 23:00:00")), 
                y = 90, label = "problem 1", angle=90, size=5, 
                colour='black', face="bold")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just so anyone might have the same question or query, here is what I did:

library(ggplot2)
library(grid)

ggplot(df, aes(Date, CPU)) + geom_point() + 
       annotate("text", x = as.POSIXct(c("2013-03-11 23:00:00")), 
                y = 90, label = "problem 1", angle=90, size=5, 
                colour='black', face="bold")
             +    geom_segment(aes(x = as.POSIXct(c("2013-03-11 09:00:00")), y = -30, xend = as.POSIXct(c("2013-03-11 09:00:00")), yend = 0), colour='#CC00FF', size=1,arrow = arrow(length = unit(0.5, "cm")))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...