在基本图形的绘图区域之外绘制图例? [英] Plot a legend outside of the plotting area in base graphics?

查看:93
本文介绍了在基本图形的绘图区域之外绘制图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述:使用基本图形时如何在图例区域之外绘制图例?

我曾考虑摆弄layout并生成一个只包含图例的空白图,但是我会对只使用基本图形工具(例如par(mar = ))在右侧获得一些空间的方式感兴趣图例的情节.

I thought about fiddling around with layout and produce an empty plot to only contain the legend, but I would be interested in a way using just the base graph facilities and e.g., par(mar = ) to get some space on the right of the plot for the legend.

这里有个例子:

plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
legend(1,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))

产生:

但是正如我所说,我希望图例位于绘图区域之外(例如,在图形/绘图的右侧.

But as said, I would like the legend to be outside the plotting area (e.g., to the right of the graph/plot.

推荐答案

也许您需要的是par(xpd=TRUE),以便可以在绘图区域之外绘制事物.因此,如果使用bty='L'进行主图绘制,则右侧将有一个图例空间.通常,这会被剪裁到绘图区域,但是进行par(xpd=TRUE)并稍加调整,就可以得到尽可能远的图例:

Maybe what you need is par(xpd=TRUE) to enable things to be drawn outside the plot region. So if you do the main plot with bty='L' you'll have some space on the right for a legend. Normally this would get clipped to the plot region, but do par(xpd=TRUE) and with a bit of adjustment you can get a legend as far right as it can go:

 set.seed(1) # just to get the same random numbers
 par(xpd=FALSE) # this is usually the default

 plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
 # this legend gets clipped:
 legend(2.8,0,c("group A", "group B"), pch = c(1,2), lty = c(1,2))

 # so turn off clipping:
 par(xpd=TRUE)
 legend(2.8,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))

这篇关于在基本图形的绘图区域之外绘制图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆