[R] legend

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Jun 22 17:26:32 CEST 2005


Thomas Steiner wrote:

> I color some area grey with polygon() (with a red border) and then I
> want to have the dashed red border in the legend as well. How do I
> manage it?
> 
> And I want to mix (latex) expressions with text in my legend.


Both points are not that easy to solve, hence I'd like to suggest to 
write your own little function that generates the legend.

Starting at the upper left, calculating the stringheight, painting the 
(party very special) symbols, and adding the text line by line seems to 
be the most easiest solution here (which is not that nice, though.

Uwe Ligges


> Just execute my lines below and you know want I mean. Or pass by at
> http://de.wikipedia.org/wiki/Bild:GBM.png to see the picture online.
> 
> Thomas
> 
> 
> bm <- function(n=500, from=0, to=1) {
>   x=seq(from=from,to=to,length=n)
>   BM<-c(0,cumsum(rnorm(n-1,mean=0,sd=sqrt(to/n))))
>   cbind(x,BM)
> }
> gbm <- function(bm,S0=1,sigma=0.1,mu=1) {
>   gbm=S0
>   for (t in 2:length(bm[,1])) {
>     gbm[t]=S0*exp((mu-sigma^2/2)*bm[t,1]+sigma*bm[t,2])
>   }
>   cbind(bm[,1],gbm)
> }
> 
> set.seed(9826064)
> cs=c("dark green", "steelblue", "red", "yellow")
> 
> #png(filename = "GBM.png", width=1600, height=1200, pointsize = 12)
> par(bg="lightgrey")
> x=seq(from=0,to=1,length=500)
> plot(x=x, y=exp(0.7*x), type="n", xlab="Zeit", ylab="", ylim=c(1,3.5))
> polygon(x=c(x,rev(x)),
> y=c(exp(0.7*x)+0.4*sqrt(x),rev(exp(0.7*x)-0.4*sqrt(x))), col="grey",
> border=cs[3], lty="dashed")
> lines(x=x,y=exp(0.7*x), type="l", lwd=3, col=cs[1])
> lines(gbm(bm(),S0=1,mu=0.7,sigma=0.4), lwd=3, col=cs[2])
> lines(gbm(bm(),S0=1,mu=0.7,sigma=0.2), lwd=3, col=cs[3])
> lines(gbm(bm(),S0=1,mu=0.7,sigma=0.1), lwd=3, col=cs[4])
> title(main="Geometrische Brownsche Bewegung",cex.main=2.5)
> legend(x=0,y=3.5,legend=c("exp(0.7x)","mu=0.7, sigma=0.4","mu=0.7,
> sigma=0.2","mu=0.7, sigma=0.1","Standardabweichung für 
> sigma=0.2"),lwd=c(4,4,4,4,12),col=c(cs,"grey"),bg="transparent",cex=1.15)
> #dev.off()
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list