调整R

时间:2017-11-28 15:57:50

标签: r image colorbar

image.plot功能自动生成输入的色标。例如,

library(fields)
x <- 1:10
y <- 1:15
z <- outer(x, y, "+")
image.plot(x, y, z)

会生成这样的数字 enter image description here 现在我想将色标调整到0到20的范围,任何大于20的值都需要着色为20。所以我在函数参数中添加了zlim=c(0,20),我得到了, enter image description here 问题是,虽然zlim调整了色阶范围,但超出限制的值未指定颜色。我怎样才能解决这个问题?在这种情况下,我希望右上角为深红色,即从颜色范围的末尾指定颜色。

3 个答案:

答案 0 :(得分:1)

您可以使用z[z > 20] <- 20来实现image.plot

library(fields)
x <- 1:10
y <- 1:15
z <- outer(x, y, "+")
z[z > 20] <- 20
image.plot(x, y, z)

enter image description here

答案 1 :(得分:1)

这是一个稍微复杂些的解决方案,它可能允许对刻度轴进行附加控制:

library(sinkr) # https://github.com/marchtaylor/sinkr
x <- 1:10
y <- 1:15
z <- outer(x, y, "+")

ncolors <- 64
colors <- jetPal(ncolors)
zlim <- c(0,20)
breaks <- c(seq(zlim[1], zlim[2], length.out = ncolors), 1e6)


op <- par(no.readonly = TRUE)
layout(matrix(1:2,1,2), widths = c(4,1), heights = 4)
par(mar = c(4,4,1,1), cex = 1)
image(x, y, z, col = colors, breaks = breaks, zlim = zlim)
par(mar = c(4,0,1,4))
imageScale(z = z, ylim = zlim, zlim = zlim, col = colors, breaks = breaks, 
  axis.pos = 4)
par(op)

enter image description here

在这里,我展示了如何指定刻度轴标签(例如,添加>):

# additional control
op <- par(no.readonly = TRUE)
layout(matrix(1:2,1,2), widths = c(4,1), heights = 4)
par(mar = c(4,4,1,1), cex = 1)
image(x, y, z, col = colors, breaks = breaks, zlim = zlim)
par(mar = c(4,0,1,4))
imageScale(z = z, ylim = zlim, zlim = zlim, col = colors, breaks = breaks, 
  axis.pos = 4, add.axis = FALSE)
axis(side = 4, at = seq(0,20,5), 
  labels = paste0(c(rep("", 4), ">"), seq(0,20,5)), las = 2)
mtext(text = "z", side = 4, line = 3)
par(op)

enter image description here

答案 2 :(得分:0)

我设法通过使用来自网格包的levelplot函数来获得一个解决方案。

grid=expand.grid(x=x,y=y)
grid$z=as.vector(z)
levelplot(z~x*y,grid,at=c(seq(0,20,length.out = 50),Inf),col.regions = tim.colors(n = 64))

结果如下,

enter image description here

但是,我仍然想知道是否可以使用image.plot或filled.contour来实现。当我尝试添加基本情节时,levelplot似乎很麻烦。