Hi
Uwe Ligges wrote: > 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. I don't think it's too bad. For example, try replacing the original ... 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) ... with ... # Use grid and gridBase so you've got some sensible # coordinate systems to work within library(grid) library(gridBase) # Align a grid viewport with the plotting region vps <- baseViewports() pushViewport(vps$inner, vps$figure, vps$plot) # Define labels and colours # Labels are mathematical expressions labels <- expression("exp(0.7x)", list(mu == 0.7,sigma == 0.4), list(mu == 0.7,sigma == 0.2), list(mu == 0.7, sigma == 0.1), paste("Standardabweichung für ",sigma == 0.2)) cols <- cs # Draw each legend item on its own line # Top line 1cm in from top-left corner for (i in 1:5) { x <- unit(1, "cm") y <- unit(1, "npc") - unit(1, "cm") - unit(i, "lines") if (i < 5) { grid.lines(unit.c(x, unit(2, "cm")), y + unit(0.5, "lines"), gp=gpar(col=cols[i], lwd=3)) } else { grid.rect(x, y, width=unit(1, "cm"), height=unit(1, "lines"), gp=gpar(fill="grey", col=cs[3], lty="dashed"), just=c("left", "bottom")) } grid.text(labels[i], x + unit(1.5, "cm"), y, just=c("left", "bottom")) } # clean up popViewport(3) ... that's a bit of typing, but if you need to do more than one, it would go inside a function with labels and cols as arguments (and '5' replaced by 'length(labels)') without too much trouble. (In this case, you could also pretty easily just do the main plot using grid and avoid having to use gridBase.) Paul >>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@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 > > > ______________________________________________ > R-help@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 -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ ______________________________________________ R-help@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