Adding labels to vertical lines when using geom_vline

13,933 views
Skip to first unread message

Tahir Butt

unread,
Oct 9, 2009, 5:55:08 PM10/9/09
to ggplot2
I'm unclear on how best to add labels to the vertical lines in
documentation for ?geom_vline.

For this I'm just using the mtcars dataset.

> library(ggplot2)
> ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_vline(xintercept = c(1.5, 3.5))

I'd like the plot to have the label "Event 1" for the line at x=1.5
and "Event 2" for x=3.5. These labels hopefully would be drawn at the
top of the plot, sort of like the x axis label.

Can someone clue me in? Should I be using annotate, geom_text, some
geom_vline argument?

I'm new to R, so I might not get it the first time around.

Thanks!
Tahir

smu

unread,
Oct 10, 2009, 4:45:07 AM10/10/09
to Tahir Butt, ggplot2
Hey,

On Fri, Oct 09, 2009 at 02:55:08PM -0700, Tahir Butt wrote:
>
> I'm unclear on how best to add labels to the vertical lines in
> documentation for ?geom_vline.
>
> For this I'm just using the mtcars dataset.
>
> > library(ggplot2)
> > ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_vline(xintercept = c(1.5, 3.5))
>
> I'd like the plot to have the label "Event 1" for the line at x=1.5
> and "Event 2" for x=3.5. These labels hopefully would be drawn at the
> top of the plot, sort of like the x axis label.

maybe this way?

p <- your_plot
p + annotate("text",x=c(1.5,3.5),y=35,label=c("Event1","Event2"),hjust=0)

Maybe you have to calculate the y position based on max(mtcars$mpg).


regards,
stefan

Tahir Butt

unread,
Oct 10, 2009, 9:59:12 AM10/10/09
to ggplot2
> p <- your_plot
> p + annotate("text",x=c(1.5,3.5),y=35,label=c("Event1","Event2"),hjust=0)

I have tried annotate but doesn't work well in my case. The labels on
the lines can easily end up overlapping if the vertical lines yet.

I'm thinking that the solution is to add an axis or add to the current
x axis additional ticks with the appropriate labels. This combined
with the geom_vline would work well.

Another complication to what I'm going for is that I'm using
facet_grid to split up the chart as follows:

> ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_vline(xintercept = c(1.5, 3.5)) + facet_grid(gear ~ .) + annotate("text",x=c(1.5,3.5),y=max(mtcars$mpg),label=c("Event1","Event2"),hjust=0)

Using annotate in this situation is repeating the labels on each
facet. Again, I'm thinking putting the labels on the x axis would
resolve this issue.

Can help with making this adjustment to the x axis or creating a
secondary axis or point me in the right direction?

Tahir

baptiste auguie

unread,
Oct 10, 2009, 10:09:17 AM10/10/09
to Tahir Butt, ggplot2
Hi,

something like this might work for you,

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() + geom_vline(xintercept = c(1.5, 3.5)) +
facet_grid(gear ~ .) +
scale_x_continuous(breaks=c(2:5,1.5,3.5), labels=c(2:5,"Event1","Event2"))

HTH,

baptiste

smu

unread,
Oct 10, 2009, 10:17:40 AM10/10/09
to Tahir Butt, ggplot2
hi,

On Sat, Oct 10, 2009 at 06:59:12AM -0700, Tahir Butt wrote:
> ...


>
> Can help with making this adjustment to the x axis or creating a
> secondary axis or point me in the right direction?
>

I don't think there is a possibility to add a seccond x-axis with
ggplot, but you can define your own axis breaks and labels.

<yourplot> + scale_x_continuous("wt",breaks=c(1.5,3.5),labels=c("Event1","Event2"))

But this removes to old labels and you'll have to include them in
scale_x_continuous. The problem with overlapping labels can also occure
on the x-axis, but maybe choosing different y values with annotate can
prevent that.


regards,
Stefan

Reply all
Reply to author
Forward
0 new messages