[R] legend

Thomas Steiner e9826064 at student.tuwien.ac.at
Wed Jun 22 13:44:09 CEST 2005


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.

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




More information about the R-help mailing list