创建看起来像IDW插值的连续热图

时间:2017-04-28 07:23:52

标签: r ggplot2 interpolation heatmap

我正在尝试使用ggplot2在R中绘制热图,但插值图太平滑了。

link for the data

我目前的代码是:

library(ggplot2)
library(xlsx)
setwd("G:\\ggplot_tables")
df = read.xlsx("heatmap.xlsx",sheetName = "Sheet1",as.data.frame=TRUE)
v = ggplot(df,aes(Zone,Year,z=Value))
v+geom_raster(aes(fill = Value),interpolate = T)+scale_fill_gradientn(colours = terrain.colors(10))                                     

结果输出过于平滑,如下所示。 enter image description here

我想达到如下所示的结果

enter image description here

从图中可以看出,使用IDW插值来绘制图。 我也尝试过使用等间距点shapefile

的R中的IDW插值

我的代码和输出如下所示

link for the shape file

library(rgdal)
library(gstat)
data.shape = readOGR(dsn = getwd(), layer = "heatmap")
grd<- as.data.frame(spsample(data.shape, "regular", n=10500))
names(grd)       <- c("X", "Y")
coordinates(grd) <- c("X", "Y")
gridded(grd)     <- TRUE
fullgrid(grd)    <- TRUE
proj4string(grd) <- proj4string(data.shape)
P.idw <- gstat::idw(Value ~ 1, data.shape, newdata=grd, idp=2.0)
plot(P.idw, zlim = c(90,170))

enter image description here

IDW结果接近我想要实现的目标。但问题是我无法更改X-axis to zoneY- axis to Year的值。

如上所示,任何有助于实现结果的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:1)

最后,我能够解决这个问题...

首先,IDW插值的输出转换为栅格

Tag

然后用r = raster(P.idw) 绘制,然后用sppplot

替换标签

对于years on y axis and zones on x axis在x和y轴上的确切位置,lat和long位置在label tics

中单独定义

最终绘图代码

使用x.scale and y.scale

定义调色板
RColorBrewer

在X和Y轴上定义tic位置

colfunc <- colorRampPalette(c("dark green","yellow","red"))

使用sppplot进行绘图

x.scale = c(7284065,7284299.1359,7284530.79226,7284762.44862,7284994.10498,7285225.76134,7285459)
y.scale = c(3570783,3571017.19943,3571248.85579,3571480.51215,3571712.16851,3571943.82486,3572175.48122,3572407.13758,3572638.79394,3572870.4503,3573102.10666,3573333.76301,3573565.41937,3573797.07573,3574029)

输出

enter image description here