在 R session 中,给出:
foo <- matrix(1:25, 5, 5)
image(foo)
为 foo
的 image()
图上使用的颜色渐变添加图例或键的最佳方法是什么?
这是针对全局降水量值的大型数据集,因此将某些内容与 legend()
一起破解似乎不是一个可行的选择。 Filled.contour() 有许多我不满意的副作用。我使用 image()
因为它是分层或添加的最简单的绘图方法。
目前,我使用 filled.contour()
的最大问题是我尝试通过 contour()
将来自不同数据集的轮廓添加到绘图中。使用 fill.contour() 时,需要调整轮廓以考虑绘图一侧的默认渐变键,但我认为如果我添加一个键也会出现这种情况image()
绘图。
非常感谢您抽出时间。
供将来引用:
使用 filled.contour()
时,除了任何其他函数之外,您还可以调用 contour()
和/或 map()
您喜欢将其分配给filled.contour()
的plot.axes
参数。记住可以用大括号堆叠多行代码可能会有所帮助。
最佳答案
这里有一些改编自 zernike 包的代码。您可以全部使用,或者只是拉出创建渐变键的部分。
# 由 CGWitthoft 于 2011 年 4 月 13 日撰写。留意来自
的更新
# zernike 包的所有者。
pupilplot <- function (wf, cp = NULL, col = topo.colors(256), addContours = FALSE,
cscale = TRUE, ...)
{
if (cscale) {
mar.orig <- (par.orig <- par(c("mar", "las", "mfrow")))$mar
on.exit(par(par.orig))
w <- (3 + mar.orig[2]) * par("csi") * 2.54
layout(matrix(c(2, 1), ncol = 2), widths = c(1, lcm(w)))
par(las = 1)
mar <- mar.orig
mar[4] <- mar[2]
mar[2] <- 1
par(mar = mar)
thelist <- list(...)
findz <- which(names(thelist) == 'zlim')
if (length(findz) > 0 ) {
zlim <- thelist$zlim
}else{
zlim <- range(wf, finite = TRUE) #the original line
}
# end of my hack
levels <- seq(zlim[1], zlim[2], length = length(col))
plot.new()
plot.window(xlim = c(0, 1), ylim = range(levels), xaxs = "i", yaxs = "i")
rect(0, levels[-length(levels)], 1, levels[-1], col = col, density = NA)
axis(4)
box()
mar <- mar.orig
mar[4] <- 0
par(mar = mar)
}
if (is.null(cp)) {
axis1 <- 1:nrow(wf)
axis2 <- 1:ncol(wf)
}
else {
axis1 <- ((1:nrow(wf)) - cp$xc)/cp$rx
axis2 <- ((1:ncol(wf)) - cp$yc)/cp$ry
}
image(axis1, axis2, wf, col = col, asp = 1, xlab = "X", ylab = "Y", ...)
if (addContours)
contour(axis1, axis2, wf, add = TRUE)
}
关于r - 为 R 图像中的颜色渐变创建图例、键或色带(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8115247/