2011-11-13 51 views
2

在R对话,:创建R中的形象赋予了传奇,关键还是颜色渐变的颜色渐变()

foo <- matrix(1:25, 5, 5) 
image(foo) 

什么是我补充一个传说或键颜色的最佳方法fooimage()图上使用的梯度?

这是一个大的数据集与legend()全球降水使黑客东西一起似乎不是一个可行的选择。 filled.contour()有一些副作用,我不满意。我使用image(),因为它是分层或添加到最简单的绘图方法。

目前,我与filled.contour()最大的问题是,我试图通过contour()将不同数据集的轮廓添加到图中。使用filled.contour()时,需要调整轮廓以考虑剧情一侧的默认渐变键,但我想如果我在image()图上添加了一个键,情况也会如此。

谢谢你的时间。

以供将来参考:

使用filled.contour(),除了可以给你的将其分配给filled.contour()plot.axes说法像其他函数调用contour()和/或map()。记住您可以用大括号堆叠多行代码,这可能会有所帮助。

+0

你可以编辑你的问题来指定'filled.contour'的不良副作用是什么吗?从'filled.contour'减去可能比添加到'image'更容易。 – joran

+0

目前,我最大的问题是,我想通过'轮廓添加从不同的数据集轮廓()'的情节。随着'filled.contour()',轮廓将需要调整,以考虑对剧情的侧面默认斜键。 – brews

回答

3

下面是一些代码,改编自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) 
}