是否有可能inset自动获得适当的值,以便图例的左角始终位于图的右上角之外?
在下面的图中我必须inset手动尝试几个值.不必手动操作会很好,因为我必须制作多个图.
graphics.off()
windows(width = 5, height = 5)
set.seed(42)
par(mar = c(5,5,1,10))
plot(rnorm(50,15,5), rnorm(50,15,3),
xlim = c(0,30), ylim = c(5,25),
pch = 19, col = c("red","blue"))
par(xpd = TRUE)
legend("topright", inset = c(-.80, 0),
pch = 19, col = c("red","blue"),
legend = c("LEGEND 1","Second Legend"))
Run Code Online (Sandbox Code Playgroud)
在之后plot的呼叫,加入之前legend,使用par("usr")*提取绘图区域的坐标.
然后,inset使用x和y使用从中获得的绘图区域的右上角坐标,而不是使用"关键字"来定位图例par("usr").x用合适的系数调整.
coord <- par("usr")
legend(x = coord[2] * 1.05, y = coord[4],
pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"))
Run Code Online (Sandbox Code Playgroud)
而且只是为了好玩,一个更复杂的选择.
在plotting 之后,legend使用位置调用topright,但不将其绘制到设备(plot = FALSE),并将其分配给对象.
提取左侧x和顶部y图例框中的坐标,并且它的宽度(参照值在节?legend),在要使用的x和y在legend:
leg <- legend("topright", pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"),
plot = FALSE)
legend(x = (leg$rect$left + leg$rect$w) * 1.05, y = leg$rect$top,
pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"))
Run Code Online (Sandbox Code Playgroud)
*从 ?par
usrc(x1, x2, y1, y2)给出绘图区域的用户坐标的极值的形式的向量.
inset在指定参数时进行的位置计算实际上是基于par("usr")(参见legend代码中的第188-199行).
| 归档时间: |
|
| 查看次数: |
1227 次 |
| 最近记录: |