label geom_hline

3,022 views
Skip to first unread message

Jeroen

unread,
Aug 5, 2009, 8:32:34 AM8/5/09
to ggplot2
I am looking for a way to add a label to an horizontal line over a
qplot. Is this possbile? For example, i use:

print(myPlot + geom_hline(yintercept=100,colour="red"));

And then I want to add a little title, preferebly just above the red
line on the left side in the plot, that says something like "Current
value".

smu

unread,
Aug 5, 2009, 8:36:54 AM8/5/09
to Jeroen, ggplot2
try:
+ annotate("text",x,y,label)

regards,
stefan

Jeroen Ooms

unread,
Aug 8, 2009, 11:28:46 AM8/8/09
to Jeroen, ggplot2
Thank you! Is there an easy way to annotate a text in for example the upper left corner of my plot? I have difficulties arranging this with x and y, because both scales are highly variable, and x is a date. Furthermore positioning of the text seems to depend on the size of the output device. 


Jeroen


2009/8/5 smu <s...@z107.de>

hadley wickham

unread,
Aug 11, 2009, 10:55:25 AM8/11/09
to Jeroen Ooms, ggplot2
It's not super easy, but it is possible. A reproducible example would
make it possible to suggest solutions.

Hadley
--
http://had.co.nz/

Jeroen Ooms

unread,
Aug 11, 2009, 11:04:20 AM8/11/09
to hadley wickham, ggplot2
Allright. For example, try this code:

library(ggplot2);
myData <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=GOOG&d=7&e=11&f=2009&g=d&a=7&b=19&c=2004&ignore=.csv",header=T);
myData$Date <- as.Date(myData$Date);
qplot(Date,Close,data=myData,geom='line');

Now I would like an annotation somewhere, for example in the left top
corner of the graph, that displays the result of Sys.time() (i.e. the
time at which the plot was made).



2009/8/11 hadley wickham <h.wi...@gmail.com>

hadley wickham

unread,
Aug 13, 2009, 1:40:59 PM8/13/09
to Jeroen Ooms, ggplot2
Something like this?

qplot(Date,Close, data = myData, geom = 'line') +
annotate("text", max(myData$Date), max(myData$Close),
label = Sys.time(), hjust = 1))

Hadley
--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages