将R中的图例绘制在绘图区域之外

时间:2013-04-25 06:31:27

标签: r legend legend-properties

我对R有疑问。我想在绘图区域外添加一个传说,在右侧,从东北角向南增长。所以我搜索了一些代码并将其写入我的代码中。这个代码给出的例子有效,但不是我的传说,这看起来很大。问题是如何实现传奇的正确位置。

这里有一些代码:

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

提前致谢

0 个答案:

没有答案