image.plot
功能自动生成输入的色标。例如,
library(fields)
x <- 1:10
y <- 1:15
z <- outer(x, y, "+")
image.plot(x, y, z)
会生成这样的数字
现在我想将色标调整到0到20的范围,任何大于20的值都需要着色为20。所以我在函数参数中添加了zlim=c(0,20)
,我得到了,
问题是,虽然zlim
调整了色阶范围,但超出限制的值未指定颜色。我怎样才能解决这个问题?在这种情况下,我希望右上角为深红色,即从颜色范围的末尾指定颜色。
答案 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)
答案 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)
在这里,我展示了如何指定刻度轴标签(例如,添加>
):
# 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)
答案 2 :(得分:0)