我正在尝试使用ggplot2在R中绘制热图,但插值图太平滑了。
我目前的代码是:
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))
我想达到如下所示的结果
从图中可以看出,使用IDW插值来绘制图。 我也尝试过使用等间距点shapefile
的R中的IDW插值我的代码和输出如下所示
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))
IDW结果接近我想要实现的目标。但问题是我无法更改X-axis to zone
和Y- axis to Year
的值。
如上所示,任何有助于实现结果的帮助都将受到赞赏。
答案 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)
输出