Inserting the last data point on ggplot2

7 views
Skip to first unread message

Vimal

unread,
Aug 24, 2009, 5:53:02 AM8/24/09
to ggplot2
Hi,

I am a new user to ggplot2 and I was wondering if any of you know how
to insert the last data point of the dataset as reference on the right
hand top corner of the graph.

Suppose I have a dataset with the last data point as on 1 July 2009
taking the value 48.3927. I want to insert this as "1 July 2009,
48.3927" on the right hand top corner of the graph so that readers
have a reference point for the graph.

library(tseries)
library(ggplot2)
B<-get.hist.quote("USD/
INR",provider="oanda",start="2009-01-01",end="2009-07-01")
C<-data.frame(dates=seq(as.Date("2009-01-01"),by=1,length.out=182),B)
print(ggplot(C,aes(x=dates))+geom_line(aes(y=B,colour="USD/
INR"),size=1.2)+scale_colour_manual("",c("USD/INR"="dark blue"))+opts
(legend.position=c(.3,.8),title="Exchange Rate")+labs(x=NULL,y=NULL))

Thanks!

Regards,
Vimal

Luciano Selzer

unread,
Aug 24, 2009, 12:22:12 PM8/24/09
to Vimal, ggplot2
Hi Vimal,
you could add the text with this
 
 
#being p your plot
p+geom_text(aes(x=C$dates[length(C$dates)], y=max(C$B)+5), label=paste(C$dates[length(C$dates)], C$B[length(C$B)], sep=','))
I think that should work. The code doens't look pretty, if anyone know a better solution please post it.
 
Bye
2009/8/24 Vimal <vim...@gmail.com>
--
Luciano

baptiste auguie

unread,
Aug 24, 2009, 2:07:28 PM8/24/09
to ggplot2
It's perhaps slightly prettier (more general anyway) to use a subset
of the data,

library(ggplot2)


x = 1:10
y = rnorm(10)
d = data.frame(x=x,y=y)

p =qplot(x,y, d)

p + geom_text(data=subset(d, x == 10),
mapping=aes(label=paste(x,round(y,2),sep=", ")), vjust=0)



HTH,

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

Vimal B

unread,
Aug 24, 2009, 11:54:28 PM8/24/09
to Luciano Selzer, ggplot2
Hi Luciano,

Thanks! It works.

Regards,
Vimal

2009/8/24 Luciano Selzer <luciano...@gmail.com>
Reply all
Reply to author
Forward
0 new messages