正如标题所说:当使用基础图形时,我如何在绘图区域之外绘制图例?

我想过摆弄布局并生成一个只包含图例的空图,但我对只使用基本图形设施和例如par(mar =)的方式感兴趣,以便在图例的右侧获得一些空间。


这里有一个例子:

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

生产:

但如前所述,我希望图例在绘图区域之外(例如,在图形/图形的右侧)。


尝试layout(),我在过去曾使用它来简单地创建一个空的图,在下面,适当缩放约1/4左右,并手动放置图例部分。

这里有一些关于legend()的老问题应该让您开始。


我只能提供一个已经指出的布局解决方案的例子。

layout(matrix(c(1,2), nrow = 1), widths = c(0.7, 0.3))
par(mar = c(5, 4, 4, 2) + 0.1)
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")
par(mar = c(5, 0, 4, 2) + 0.1)
plot(1:3, rnorm(3), pch = 1, lty = 1, ylim=c(-2,2), type = "n", axes = FALSE, ann = FALSE)
legend(1, 1, c("group A", "group B"), pch = c(1,2), lty = c(1,2))


也许你需要的是par(xpd=TRUE),以使事物能够在绘图区域之外绘制。所以如果你用bty='L'来做主要的情节,你会在右边留出一些空间来做图例。通常情况下,这将被剪辑到绘图区域,但做par(xpd=TRUE),并进行一些调整,你可以得到一个图例尽可能正确:

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

很抱歉复活了一个旧线程,但我今天遇到了同样的问题。我发现的最简单的方法是:

# Expand right side of clipping rect to make room for the legend
par(xpd=T, mar=par()$mar+c(0,0,0,6))

# Plot graph normally
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")

# Plot legend where you want
legend(3.2,1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))

# Restore default clipping rect
par(mar=c(5, 4, 4, 2) + 0.1)

在这里找到:http://www.harding.edu/fmccown/R/


没有人提到在图例中使用负插入值。下面是一个例子,图例位于图的右侧,与顶部对齐(使用关键字“topright”)。

# Random data to plot:
A <- data.frame(x=rnorm(100, 20, 2), y=rnorm(100, 20, 2))
B <- data.frame(x=rnorm(100, 21, 1), y=rnorm(100, 21, 1))

# Add extra space to right of plot area; change clipping to figure
par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)

# Plot both groups
plot(y ~ x, A, ylim=range(c(A$y, B$y)), xlim=range(c(A$x, B$x)), pch=1,
               main="Scatter plot of two groups")
points(y ~ x, B, pch=3)

# Add legend to top right, outside plot region
legend("topright", inset=c(-0.2,0), legend=c("A","B"), pch=c(1,3), title="Group")

inset=c(-0.2,0)的第一个值可能需要根据图例的宽度进行调整。


您可以使用Plotly R API,或者使用代码,或者从GUI上将图例拖到您想要的位置。

这里有一个例子。图表和代码也在这里。

x = c(0,1,2,3,4,5,6,7,8) 
y = c(0,3,6,4,5,2,3,5,4) 
x2 = c(0,1,2,3,4,5,6,7,8) 
y2 = c(0,4,7,8,3,6,3,3,4)

通过将x和y值中的一个赋值为100或-100,可以将图例定位到图形之外。

legendstyle = list("x"=100, "y"=1)
layoutstyle = list(legend=legendstyle)

以下是其他选择:

list("x" = 100, "y" = 0)为右下外侧 list("x" = 100, "y"= 1)右上角外侧 list("x" = 100, "y" = .5)右中外 list("x" = 0, "y" = -100 list("x" = 0.5, "y" = -100 list("x" = 1, "y" = -100) Under Right

然后是回应。

Response = p$plotly(x,y,x2,y2, kwargs=list(layout=layoutstyle));

当您进行调用时,Plotly返回带有图形的URL。您可以通过调用browseURL(response$url)来更快地访问它,这样它就会在浏览器中为您打开图形。

url = response$url
filename = response$filename

这就得到了这个图。您还可以从GUI中移动图例,然后图形将相应地缩放。坦白地说,我是Plotly团队的一员。


除了前面提到的方法(使用layout或par(xpd=TRUE))之外,另一个解决方案是在整个设备上覆盖一个透明的plot,然后添加图例。

诀窍是将一个(空的)图形覆盖在完整的绘图区域上,并将图例添加到该区域。我们可以使用par(fig=…)选项。首先,我们指示R在整个绘图设备上创建一个新的绘图:

par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)

我们需要设置oma和mar,因为我们想要让图形的内部覆盖整个设备。new=TRUE用于防止R启动新设备。然后我们可以添加空图:

plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')

我们已经准备好添加图例了:

legend("bottomright", ...)

将在设备的右下方添加一个图例。同样,我们可以将图例添加到顶部或右侧边缘。我们唯一需要确保的是,原始情节的边缘足够大,以容纳传说。

把所有这些放到一个函数中;

add_legend <- function(...) {
  opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), 
    mar=c(0, 0, 0, 0), new=TRUE)
  on.exit(par(opar))
  plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
  legend(...)
}

举个例子。首先创建情节,确保我们有足够的空间在底部添加图例:

par(mar = c(5, 4, 1.4, 0.2))
plot(rnorm(50), rnorm(50), col=c("steelblue", "indianred"), pch=20)

然后添加图例

add_legend("topright", legend=c("Foo", "Bar"), pch=20, 
   col=c("steelblue", "indianred"),
   horiz=TRUE, bty='n', cex=0.8)

导致:


最近我发现了一个非常简单和有趣的功能,可以在你想要的plot区域之外打印图例。

在这块地的右边做外层边缘。

par(xpd=T, mar=par()$mar+c(0,0,0,5))

创建一个情节

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

添加图例,只需使用定位器(1)函数如下所示。然后你只需点击你想要加载以下脚本后。

legend(locator(1),c("group A", "group B"), pch = c(1,2), lty = c(1,2))

试一试


我喜欢这样做:

par(oma=c(0, 0, 0, 5))
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(par('usr')[2], par('usr')[4], bty='n', xpd=NA,
       c("group A", "group B"), pch=c(1, 2), lty=c(1,2))

唯一需要调整的是设置正确的空白宽到足以容纳图例。

然而,这也可以自动化:

dev.off() # to reset the graphics pars to defaults
par(mar=c(par('mar')[1:3], 0)) # optional, removes extraneous right inner margin space
plot.new()
l <- legend(0, 0, bty='n', c("group A", "group B"), 
            plot=FALSE, pch=c(1, 2), lty=c(1, 2))
# calculate right margin width in ndc
w <- grconvertX(l$rect$w, to='ndc') - grconvertX(0, to='ndc')
par(omd=c(0, 1-w, 0, 1))
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(par('usr')[2], par('usr')[4], bty='n', xpd=NA,
       c("group A", "group B"), pch=c(1, 2), lty=c(1, 2))


添加另一个简单的选择,在我看来是相当优雅的。

你的阴谋:

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("bottomright", c("group A", "group B"), pch=c(1,2), lty=c(1,2),
       inset=c(0,1), xpd=TRUE, horiz=TRUE, bty="n"
       )

结果:

这里只向示例添加了图例的第二行。反过来:

inset=c(0,1) - moves the legend by fraction of plot region in (x,y) directions. In this case the legend is at "bottomright" position. It is moved by 0 plotting regions in x direction (so stays at "right") and by 1 plotting region in y direction (from bottom to top). And it so happens that it appears right above the plot. xpd=TRUE - let's the legend appear outside of plotting region. horiz=TRUE - instructs to produce a horizontal legend. bty="n" - a style detail to get rid of legend bounding box.

同样适用于在侧面添加图例:

par(mar=c(5,4,2,6))
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("topleft", c("group A", "group B"), pch=c(1,2), lty=c(1,2),
       inset=c(1,0), xpd=TRUE, bty="n"
       )

在这里,我们简单地调整了图例位置,并在图的右侧添加了额外的空白空间。结果: