Specifiying slope,intercept geom_abline using dates

575 views
Skip to first unread message

Joran

unread,
Sep 2, 2010, 1:18:45 PM9/2/10
to ggplot2
Here's my little example:

x <-
seq(from=as.Date("1989/10/15"),to=as.Date("2010/08/15"),by="months")
y <- 10*runif(length(x))
data <- data.frame(x=x,y=y)
ggplot(data,aes(x=x,y=y)) + geom_abline() + geom_point()

Not sure how to specify the slope and intercept in geom_abline()...

I've tried a few things and either nothing appears, or I get an error
(usually when trying to specify the intercept as a date using
as.Date()).

Ideas?

fernando

unread,
Sep 2, 2010, 2:32:42 PM9/2/10
to Joran, ggplot2
Hi Joran,

Using the range of your data ...

slope <- 10 / as.numeric(as.Date("2010/08/15") - as.Date("1989/10/15"))
intercept <- - slope * as.numeric(as.Date("1989/10/15"))

ggplot(data,aes(x=x,y=y)) +
geom_abline(aes(intercept = intercept, slope = slope)) +
geom_point()

Hope this helps,
fernando

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
Joran
Enviado el: jueves, 02 de septiembre de 2010 19:19
Para: ggplot2
Asunto: Specifiying slope,intercept geom_abline using dates

Ideas?

--
You received this message because you are subscribed to the ggplot2 mailing
list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Sep 2, 2010, 9:33:25 PM9/2/10
to Joran, ggplot2
Hi:

How about this?

ggplot(data,aes(x=x,y=y)) + geom_point() + geom_path() +
     geom_smooth(method = 'lm', se = FALSE)

The advantage of geom_smooth() is that you can use several types of smoothers - e.g.,  lm, loess, gam - so feel free to play with it.

HTH,
Dennis


--
Reply all
Reply to author
Forward
0 new messages