draw line between two points

6,093 views
Skip to first unread message

thomas

unread,
May 22, 2011, 1:07:23 PM5/22/11
to ggplot2
hi,

maybe i am missing something but how can i draw a simple line between
two points on top of a ggplot() plot?

thanks in advance.

best,
thomas

Scott Chamberlain

unread,
May 22, 2011, 2:06:17 PM5/22/11
to thomas, ggplot2
Is this what you mean?

ggplot(mtcars, aes(x = mpg, y = drat, colour = gear, group = gear)) +
  geom_point()
last_plot() + geom_line()


Scott
--
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

Thomas Kern

unread,
May 22, 2011, 2:18:02 PM5/22/11
to Scott Chamberlain, ggplot2
no, I meant between two points that have nothing to do with the dataset.

something like ggplot() + draw_line(c(10,10),c(29,38))

Best,
Thomas


> ------------------------------------------------------------------------
>
> Scott Chamberlain <mailto:myrmec...@gmail.com>
> 22 May 2011 8:06 PM

Heike Hofmann

unread,
May 22, 2011, 2:23:55 PM5/22/11
to Thomas Kern, Scott Chamberlain, ggplot2
Thomas,

try

ggplot() + geom_line(aes(x=c(10,10),y=c(29,38)))

Heike

Thomas Kern

unread,
May 22, 2011, 2:35:16 PM5/22/11
to Heike Hofmann, Scott Chamberlain, ggplot2
Heike,

doesn't that only work on an 'empty' plot?

i have something like this

data = data.frame(x = seq(0,50,1),y= seq(0,50,1),l = seq(100,150,1))
p = ggplot(data,aes(x=x,y=y,label=l))
p = p + geom_text(size=2)

doing

p = p + geom_line(aes(x=c(10,10),y=c(15,15))

isn't working. Throws an error complaining about the number of rows ":
2, 51". 51 probably stems from data.

Thomas

> ------------------------------------------------------------------------
>
> Heike Hofmann <mailto:heike....@gmail.com>
> 22 May 2011 8:23 PM


>
>
> Thomas,
>
> try
>
> ggplot() + geom_line(aes(x=c(10,10),y=c(29,38)))
>
> Heike
>
>
>

Scott Chamberlain

unread,
May 22, 2011, 3:53:47 PM5/22/11
to Thomas Kern, Heike Hofmann, ggplot2
How about geom_segment:

dat <- data.frame(var1 = c(1,2), var2 = c(2,1))
ggplot(dat, aes(x = var1, y = var2)) +
  geom_point()  
last_plot() + geom_segment(aes(x = 2, y = 1, xend = 1, yend = 2))

Thomas Kern

unread,
May 22, 2011, 4:11:01 PM5/22/11
to Scott Chamberlain, Heike Hofmann, ggplot2
Heike,

that actually worked, thanks.

Scott,

that seems to be working as well thanks.

Best,
Thomas

> ------------------------------------------------------------------------
>
> Scott Chamberlain <mailto:myrmec...@gmail.com>

> 22 May 2011 9:53 PM


>
>
> How about geom_segment:
>
> dat <- data.frame(var1 = c(1,2), var2 = c(2,1))
> ggplot(dat, aes(x = var1, y = var2)) +
> geom_point()
> last_plot() + geom_segment(aes(x = 2, y = 1, xend = 1, yend = 2))
>
> On Sunday, May 22, 2011 at 1:35 PM, Thomas Kern wrote:
>
>

> ------------------------------------------------------------------------
>
> Thomas Kern <mailto:thk....@gmail.com>
> 22 May 2011 8:35 PM


>
>
> Heike,
>
> doesn't that only work on an 'empty' plot?
>
> i have something like this
>
> data = data.frame(x = seq(0,50,1),y= seq(0,50,1),l = seq(100,150,1))
> p = ggplot(data,aes(x=x,y=y,label=l))
> p = p + geom_text(size=2)
>
> doing
>
> p = p + geom_line(aes(x=c(10,10),y=c(15,15))
>
> isn't working. Throws an error complaining about the number of rows ":
> 2, 51". 51 probably stems from data.
>
> Thomas
>
>
>
> ------------------------------------------------------------------------
>
> Heike Hofmann <mailto:heike....@gmail.com>
> 22 May 2011 8:23 PM
>
>
> Thomas,
>
> try
>
> ggplot() + geom_line(aes(x=c(10,10),y=c(29,38)))
>
> Heike
>
>
>
> ------------------------------------------------------------------------
>

> Thomas Kern <mailto:thk....@gmail.com>
> 22 May 2011 8:18 PM


>
>
> no, I meant between two points that have nothing to do with the dataset.
>
> something like ggplot() + draw_line(c(10,10),c(29,38))
>
> Best,
> Thomas
>
>

Reply all
Reply to author
Forward
0 new messages