简体   繁体   English

在R中的图外绘制图例

[英]Plotting legend outside plot in R

I am trying to put the legend outside the plot using xpd=TRUE but the legend keeps plotting within the plot. 我试图使用xpd=TRUE将图例放置在图外,但图例将图保留在图内。 How can I fix this? 我怎样才能解决这个问题?

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

Use inset and made xpd to true 使用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))

See the help for legend : 参见legend帮助:

The location may also be specified by setting x to a single keyword from the list "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". 也可以通过将x设置为列表“ bottomright”,“ bottom”,“ bottomleft”,“ left”,“ topleft”,“ top”,“ topright”,“ right”和“ center”中的单个关键字来指定位置”。 This places the legend on the inside of the plot frame at the given location. 这会将图例放在情节图框的内部给定位置。

So you can place the legend outside the plotting region by giving its coordinates manually: 因此,您可以通过手动指定图例的坐标来将图例放置在绘图区域之外:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM