我试图使用filled.contour在r中创建一个热图,我有一个看起来有点像下面那个的数据框,但颜色被回收,这对它来说非常无用。我做错了什么?
x <- c(seq(15, 75, by = 5))
y <- rep(c(100,200,300,400,500, 600, 700, 800),each=length(x))
z <- sample(0:34, length(y), replace=TRUE)
z <- z/10000
d <- data.frame(x, y, z)
resolution <- 0.1
a <- interp(x=d$x, y=d$y, z=d$z,
xo=seq(min(d$x),max(d$x),by=resolution),
yo=seq(min(d$y),max(d$y),by=resolution),
duplicate="mean")
MyHeatMapColours <-colorRampPalette(c("red","yellow","springgreen","royalblue"))(50)
filled.contour(a, col = MyHeatMapColours, nlevels = 49)
答案 0 :(得分:0)
我猜测interp
来自包:akima。你的例子并不是最小的,因为它需要很长的时间来运行,所以我在不失一般性的情况下减少了resiolution。我认为使用&#34; level&#34;更安全。而不是&#34; nlevels&#34;,虽然我不能告诉你为什么会这样:
library(akima) # which does have an `interp`-function
x <- c(seq(15, 75, by = 5))
y <- rep(c(100,200,300,400,500, 600, 700, 800),each=length(x))
z <- sample(0:34, length(y), replace=TRUE)
z <- z/10000
d <- data.frame(x, y, z)
resolution <- 0.5
a <- interp(x=d$x, y=d$y, z=d$z,
xo=seq(min(d$x),max(d$x),by=resolution),
yo=seq(min(d$y),max(d$y),by=resolution),
duplicate="mean")
MyHeatMapColours <-colorRampPalette(c("red","yellow","springgreen","royalblue"))(49)
#colors are supposed to be one less than length of levels.
filled.contour(a, col = MyHeatMapColours,
levels = seq(min(z),max(z), length=50))