gpt4 book ai didi

r - 自动计算图例的适当插入值

转载 作者:行者123 更新时间:2023-12-04 12:07:05 25 4
gpt4 key购买 nike

是否可以获得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"))

enter image description here

最佳答案

plot 之后在添加 legend 之前调用, 使用 par("usr") * 提取绘图区域的坐标。

然后,不要使用“关键字”和 inset 来定位图例。 , 使用 xypar("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"))

enter image description here

只是为了好玩,一个更复杂的选择。

之后 plot婷,调用 legend与位置 topright ,但不将其绘制到设备( plot = FALSE ),并将其分配给对象。

提取左边 x和顶部 y图例框的坐标及其宽度(参见 ?legend 中的值部分),用于 xylegend :
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"))

enter image description here

*来自 ?par

usr A vector of the form c(x1, x2, y1, y2) giving the extremes of the user coordinates of the plotting region.


inset 时进行的位置计算已指定参数,实际上基于 par("usr") (见 legend code 中的第 188-199 行)。

关于r - 自动计算图例的适当插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42075751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com