R中带有等高线的热图

时间:2021-03-12 15:16:05

标签: r heatmap contour lattice

您好,我想从包含三列的表中生成热图。

DF:

 z         x    y
 0.014514491  21.340950 0.26666667
 0.013282926  12.907711 0.30434783
 0.020464170  10.266667 0.06250000
 0.039909394  25.778167 0.00000000
 0.023635756  18.537568 0.07142857
 0.078711706  13.441777 0.07142857
 0.084150460   6.622687 0.07142857
 0.111338834   1.910518 0.05263158
 0.066590831   9.889781 0.05882353

我想绘制这三列以获得具有这样的等高线的热图,但我想要有黑点和等高线的标签,我希望缩短为只有 4 位数字。

enter image description here

我的代码使用 R 基础,但我不知道如何将 z 值转换为矩阵以用于 fill.contour 命令:

library(RColorBrewer)

levels1 <- pretty(range(DF$z, finite = TRUE), 40)
col1 <- rev(colorRampPalette(colors = c("red", "yellow", "white"), bias = 2)(n = length(levels1) - 1))
# Plot for DF
plot(x = DF$x, y = DF$y, type = "n", xaxs = "i", yaxs = "i", xlab = "", ylab = "", axes = FALSE)
Axis(side=1, labels=TRUE)
Axis(side=2, labels=TRUE)
# Contour fill
with(DF, .filled.contour(x = x, y = y, z = matrix(z, 9,9), levels=levels1, col=col1))
# Contour lines
with(DF, contour(x = x, y = y, z = matrix(z, 9, 9), nlevels = 40, add = TRUE))
# Box around plot
box()
# Add points
with(DF, points(x = x, y = y, pch = 21, bg = "black"))

contour.fill 命令后我的错误:

Error in .filled.contour(x = Ne, y = im.rate, z = matrix(Pop_spec_fst,  : 
  invalid x / y values or limits

或者我使用的是格子,但我不知道如何将点变为黑色和轮廓标签:

library(latticeExtra) 
library(RColorBrewer) 
library(rasterVis)

levelplot(z ~ x * y, DF, 
          panel = panel.levelplot.points, cex = 1.2, cuts = 15, contour = TRUE, labels=list(cex=0.7, round(DF$z, 4)),col="black", par.settings = RdBuTheme
) + 
  layer_(panel.2dsmoother(..., n = 500)) 

0 个答案:

没有答案