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