I have data consisting of (x,y,z) values, and I would like to plot it as a heatmap in ggplot2. However, these points are not evenly spaced (i.e, the x and y datapoints do not fall nicely onto a regular grid), which prevents the data from being displayed normally. I have found this post regarding a similar question, in which they suggest using the interp
function to interpolate the data to an evenly-spaced grid. I have included some example code below that accomplishes this
library(akima)
data <- data.frame("x" = c(1, 3, 5), "y" = c(-1, 2, 8), "z" = c(5, 1, 2))
resolution <- 0.1
a <- interp(x=data$x, y=data$y, z=data$z,
xo=seq(min(data$x),max(data$x),by=resolution),
yo=seq(min(data$y),max(data$y),by=resolution), duplicate="mean")
the resulting variable a
can be displayed automatically using the image
function, but I would like to use ggplot2. I cannot understand how a
is structured - it appears to be a list of lists, but each of the lists are of different lengths and I am unsure how to cast it into a form suitable for ggplot2. I have not been able to find an answer to this in the documentation for interp
. Can someone explain how to recast a
in a way that it can be plotted using ggplot2?