Two-dimensional interpolation in R, without any extrapolation -
i have 2-dimensional array of data, missing values. there 3 columns:
- x
- y
- intensity
i can plot x against y in ggplot2, intensity colour scale.
i’d smooth transitions between colours, , have come across idw
function, gstat
package. idw
aims interpolate nas in 2-dimenstions. shouldn’t extrapolate, , whilst technically respect limits of data (±20 in both directions), makes attempt fill gaps @ edge of plot, seen below:
i’d avoid extrapolation occurring outside limits of data have, including bottom-right of data shown in first figure.
how might achieve this?
edit: here example dataset. isn't same dataset shown above, again contains large missing region in lower-right corner.
structure(list(x = c(10l, 15l, -10l, 0l, -5l, -10l, -15l, 0l, -15l, 15l, 5l, 10l, -20l, -5l, -15l, -15l, -5l, 5l, 20l, -20l, -15l, 20l, -15l, 5l, -5l, -20l, -5l, 15l, 0l, 0l, 15l, 10l, 0l, 20l, -10l, 5l, 5l, 0l, 20l, 5l, -15l, 5l, -5l, -5l, -15l, -10l, -10l, -10l, -5l, -10l, 15l, 20l, 0l, 20l, -15l, 20l, -20l, -15l, 10l, 15l, 15l, -5l, 5l, 15l, 20l, 20l, -10l, -20l, -20l, 15l, -10l, 10l, 5l, -20l, 20l, 10l, 0l, 10l, -10l, 0l, 10l, 10l, 10l, -20l, 15l, -20l, 0l, -20l, -5l, 5l), y = c(0l, -10l, 0l, 20l, 0l, -10l, 0l, 0l, -20l, 20l, 0l, -10l, -10l, -10l, -10l, 20l, 10l, -10l, -20l, -20l, -10l, -10l, 0l, 10l, -20l, 20l, 0l, 0l, 0l, -20l, 0l, 0l, 10l, 10l, -20l, -20l, -10l, 20l, 10l, 20l, 10l, -20l, 20l, -10l, 20l, 20l, 10l, 10l, -20l, -10l, -10l, 20l, -10l, -10l, -20l, 0l, -10l, 10l, -10l, 10l, -20l, 10l, 20l, 20l, -20l, 20l, 0l, 10l, 10l, -20l, 20l, -20l, 10l, 0l, 0l, 10l, 10l, -20l, -20l, -20l, 20l, 20l, 10l, 20l, 10l, -20l, -10l, 0l, 20l, 0l), intensity = c(12.9662, na, 24.4379, 26.3923, 26.9449, 16.7372, 13.7691, 8.029, 11.922, 11.1967, 15.2792, na, 14.4159, 20.6542, 22.0509, 17.356, 14.3841, na, na, 10.326, 6.0451, na, 12.9515, 3.6745, na, 18.1552, 9.9532, 9.9361, 7.0392, na, 10.9814, 10.8351, 4.9017, 5.7864, 14.098, na, na, 6.3305, 6.4405, 49.2791, 19.9774, na, 25.1955, 28.5234, 20.2077, 20.3224, 12.688, 22.1371, na, 17.5108, na, 7.9351, na, na, 11.0975, 8.2349, 12.1194, 21.865, na, 10.7178, na, 21.8222, 13.5971, 6.9751, na, 8.8046, 22.0709, 14.2043, 27.8561, na, 17.4329, na, 7.4057, 15.2797, 1.0122, 11.1874, 35.5814, na, 27.5919, na, 11.8159, 15.8433, 12.297, 29.1978, 20.4151, 22.6336, na, 16.0019, 16.9746, 10.8613)), .names = c("x", "y", "intensity"), row.names = c(na, -90l), class = "data.frame")
if understand trying do, can base methods in ggplot2 removing na prior interpolating.
library(ggplot2) data<-data.frame(data) data_na.rm<-data[!is.na(data$intensity),] ggplot(data=data_na.rm,aes(x=x,y=y))+ geom_raster(aes(fill=intensity),interpolate=true)
results in:
Comments
Post a Comment