0

我有问题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)

提前致谢

4

0 回答 0