在R中的情节外绘制图例

时间:2018-06-07 09:06:58

标签: r plot legend

我正在尝试使用xpd=TRUE将图例放在情节之外,但是图例会在情节中进行绘图。我该如何解决这个问题?

x = c(0,0,1,1,1)
y = c(0.4991,1.1423,1.2258,1.158,0.5148)
dat<-cbind(x,y)
point_shape = c(10,15,10,15,1)
dat<-data.frame(x,y,point_shape)

myTicks<-c(0,1)
plot(dat[,1],dat[,2], yaxt="n", xaxt="n", xlab="", ylab="",pch = dat$point_shape)
abline(0.4991,0.7267)
abline(1.1423,0.0157)
abline(0.4991,0.0157,lty=2)
axis(side = 1, at = myTicks)
axis(side = 2, at = myTicks)


legend("bottomleft", legend = c("apple", "orange", "tree"),
       bty = "n", xpd=FALSE, mar(c(7,7,7,7)), cex = 1, pch = c(10, 15, 1))

2 个答案:

答案 0 :(得分:2)

使用inset并使xpd为true

legend("bottomleft", legend = c("apple", "orange", "tree"), inset=c(-0.15,0),
   bty = "n", xpd=TRUE, mar(c(7,7,7,7)), cex = 1, pch = c(10, 15, 1))

答案 1 :(得分:0)

请参阅legend的帮助:

  

也可以通过将x设置为单个关键字来指定位置   从列表&#34; bottomright&#34;,&#34; bottom&#34;,&#34; bottomleft&#34;,&#34; left&#34;,   &#34; topleft&#34;,&#34; top&#34;,&#34; topright&#34;,&#34; right&#34;和&#34;中心&#34;。这放置了   在给定位置的情节框架内侧的图例。

因此,您可以通过手动提供坐标将图例放置在绘图区域之外:

legend(-0.2, 0.3, legend = c("apple", "orange", "tree"),
       bty = "n", xpd=TRUE, mar=c(7,7,7,7), cex = 1, pch = c(10, 15, 1))