I've a problem with R
. I want to add a legend outside the plotting area, on the right side, growing from the northeast corner southward. So I searched for some code and wrote it to my code. The example given with this code works, but not with my legend, which seems to be to big. The question is how to achieve the right position of the legend.
Here's some code for you:
#Construct some data and start the plot
x <- 0:64/64
y <- sin(3*pi*x)
plot(x, y, type="l", col="blue")
points(x, y, pch=21, bg="white")
#Grab the plotting region dimensions
rng <- par("usr")
#Call your legend with plot = FALSE to get its dimensions
lg <- legend(rng[1],rng[4] + lg$rect$h,xpd = NA,y.intersp=.75,c("Datenreihe 1", "Datenreihe 2", "lineare Regression\nfür Datenreihe 1", "lineare Regression\nfür Datenreihe 2"), pch = c(1,1,NA,NA), lty = c(NA,NA,1,1), col=c("lightblue","lightgreen","darkblue","darkgreen"),plot = FALSE)
#Once you have the dimensions in lg, use them to adjust
#the legend position
#Note the use of xpd = NA to allow plotting outside plotting region
legend(rng[1],rng[4] + lg$rect$h,xpd = NA,y.intersp=.75,c("Datenreihe 1", "Datenreihe 2", "lineare Regression\nfür Datenreihe 1", "lineare Regression\nfür Datenreihe 2"), pch = c(1,1,NA,NA), lty = c(NA,NA,1,1), col=c("lightblue","lightgreen","darkblue","darkgreen"),plot = TRUE)
Thanks in advance