geom_segment with fixed characteristics?

298 views
Skip to first unread message

Ben Bolker

unread,
Nov 9, 2010, 11:48:57 AM11/9/10
to ggplot2
(Having answered a few questions here, I feel entitled to ask my own
boneheaded question now ...)

Dear ggplot'ers,

I'm trying to annotate a plot.
From the code below, I expected to see a red line running diagonally
partway across the plot.
What am I missing ... ?
(geom_abline is added here just as a separate trial, to ensure that
I really can add layers)

library(ggplot2)
t1 <- ggplot(data.frame(x=runif(10,max=30),y=runif(10,max=0.05)),
aes(x=x,y=y))+xlim(0,30)+ylim(0,0.05)+geom_point()
t1+geom_segment(data=NULL,x=1,x.end=20,y=0.01,y.end=0.04,linetype=2,colour="red")+
geom_abline(intercept=0,slope=0.01,colour="blue")

cheers
Ben Bolker

Brandon Hurr

unread,
Nov 9, 2010, 11:55:24 AM11/9/10
to Ben Bolker, ggplot2
I'm not sure if this is the answer you're looking for since the line isn't going across the middle of the plot like you suggested but putting the x,xend,y, yend in aes() it seems to work...

library(ggplot2)
t1 <- ggplot(data.frame(x=runif(10,max=30),y=runif(10,max=0.05)),
            aes(x=x,y=y))+xlim(0,30)+ylim(0,0.05)+geom_point()
t1+geom_segment(data=NULL,aes(x=1,xend=20,y=0.01,yend=0.04),linetype=2,colour="red")+
geom_abline(intercept=0,slope=0.01,colour="blue")

On Tue, Nov 9, 2010 at 16:48, Ben Bolker <bbo...@gmail.com> wrote:
geom_segment(data=NULL,x=1,x.end=20,y=0.01,y.end=0.04,linetype=2,colour="red")

When I tried the geom_segment call on its own it didn't like x.end or y.end, it seemed to want xend and yend. 

Brandon 

red-blue.png

Luciano Selzer

unread,
Nov 9, 2010, 11:55:45 AM11/9/10
to Ben Bolker, ggplot2
Dear Ben, you forgot to make the aes call in geom segment and it's xend and yend. No point in the middle.
Here's the right code


library(ggplot2)
t1 <- ggplot(data.frame(x=runif(10,max=30),y=runif(10,max=0.05)),
            aes(x=x,y=y))+xlim(0,30)+ylim(0,0.05)+geom_point()
t1+geom_segment(data=NULL, aes(x=1,xend=20,y=0.01,yend=0.04),linetype=2,colour="red")+
geom_abline(intercept=0,slope=0.01,colour="blue")

HTH
Luciano


2010/11/9 Ben Bolker <bbo...@gmail.com>

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

Fernando Gonzalez

unread,
Nov 9, 2010, 11:56:32 AM11/9/10
to Ben Bolker, ggplot2
Hi Ben,

'aes' within geom_segment and avoid the dots in 'x.end' and 'y.end'?

library(ggplot2)
t1 <- ggplot(data.frame(x=runif(10,max=30),y=runif(10,max=0.05)),
            aes(x=x,y=y))+xlim(0,30)+ylim(0,0.05)+geom_point()
t1+geom_segment(data=NULL,aes(x=1,xend=20,y=0.01,yend=0.04),linetype=2,colour="red")+
geom_abline(intercept=0,slope=0.01,colour="blue")

2010/11/9 Ben Bolker <bbo...@gmail.com>

Hadley Wickham

unread,
Nov 9, 2010, 2:37:37 PM11/9/10
to Luciano Selzer, Ben Bolker, ggplot2
> Dear Ben, you forgot to make the aes call in geom segment and it's xend and
> yend. No point in the middle.

You shouldn't have to put the values in the aesthetics, but I think
there's a bug in that case where there is no data and all aesthetics
are set, not mapped.

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Ben Bolker

unread,
Nov 9, 2010, 2:48:18 PM11/9/10
to Hadley Wickham, Luciano Selzer, ggplot2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 11/09/2010 02:37 PM, Hadley Wickham wrote:
>> Dear Ben, you forgot to make the aes call in geom segment and it's xend and
>> yend. No point in the middle.
>
> You shouldn't have to put the values in the aesthetics, but I think
> there's a bug in that case where there is no data and all aesthetics
> are set, not mapped.
>
> Hadley
>
>

Good to know I was only being a little bit boneheaded. Thanks for the
help everyone.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkzZpYIACgkQc5UpGjwzenODVACgkhdqvrKUKrd/2A0k/loFmYKL
U7MAn279vJ+RulJGLBBYMYQyCkTHFlIc
=CCd8
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages