Reputation: 517
I am trying to plot heat map in R with ggplot2 but the interpolated plot is too much smoothed.
my current code is:
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))
The resulted output is too much smoothed as shown below.
I want to achieve results as shown below
It appears from the plot that IDW interpolation is used to prepare the plot. I have also tried IDW interpolation in R with equally spaced point shapefile
My code and output is shown below
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 result is close to what I am trying to achieve.But the problem is that I am not able to change the values of X-axis to zone
and Y- axis to Year
.
Any help to achieve the result as shown above will be appreciated.
Upvotes: 1
Views: 2551
Reputation: 517
Finally, I am able to solve this problem...
First, the Output of the IDW interpolation was converted to raster
r = raster(P.idw)
and then plotted with sppplot
and then lables are replaced with the years on y axis and zones on x axis
for exact position of label tics
on the x and y axis there, lat and long position are defined separately in x.scale and y.scale
final plotting code
defining color palette using RColorBrewer
library
colfunc <- colorRampPalette(c("dark green","yellow","red"))
defining tic locations on X and Y axis
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)
Plotting using sppplot
spplot(r,scales=list(x=list(x.scale,labels=c(seq(1,7,1))), y=list(y.scale,labels=c(seq(2001,2015,1))),col.regions =colfunc,at=seq(100, 170, len=100), xlab = "Elevation Zone", ylab = "year")
Output
Upvotes: 2