稍微脏一点的 hack 可以让您获得 legend() 函数,从而为您提供必要的信息。比我更聪明的人可能会计算出legend() 如何计算框定位并将其复制到函数之外。请注意,可能不建议编辑标准 R 函数。
如果您还没有编辑过 R 函数,一个简单(和临时)的访问它的方法是输入
fix(legend)
打字
rm(legend)
稍后将撤消您的更改。
找到写有fill <- rep 的部分并添加由 cmets 指示的行:
fillList <- NULL ## added
if (mfill) {
if (plot) {
fill <- rep(fill, length.out = n.leg)
rect2(left = xt, top = yt + ybox/2, dx = xbox, dy = ybox,
col = fill, density = density, angle = angle,
border = border)
fillList <- data.frame(left = xt, top = yt + ybox/2, dx = xbox, dy = ybox) ## added
}
xt <- xt + dx.fill
}
找到最后一行并将其更改为
invisible(list(rect = list(w = w, h = h, left = left, top = top),
text = list(x = xt, y = yt), fillList=fillList)) ## modified
现在通过
调用图例
output <- legend(...) ## replace ... with whatever you want to have as inputs
并使用legend()返回的信息绘制三角形,如下所示:
with(output$fillList[1,], { ## first box
polygon(c(left, left+dx, left+dx), c(top, top, top-dy), col=myColour, border=NA)
})