正如标题所说:当使用基础图形时,我如何在绘图区域之外绘制图例?
我想过摆弄布局并生成一个只包含图例的空图,但我对只使用基本图形设施和例如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))
生产:
但如前所述,我希望图例在绘图区域之外(例如,在图形/图形的右侧)。
也许你需要的是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)的第一个值可能需要根据图例的宽度进行调整。
也许你需要的是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))
我只能提供一个已经指出的布局解决方案的例子。
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))
最近我发现了一个非常简单和有趣的功能,可以在你想要的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))
试一试