R levelplot, non-continuous latitude and longitude values
I have data-set represented as latitude-longitude and a VALUE(named "class") associated with each latitude-longitude pair, which i want to represent as using levelplot() or contourplot() under the "lattice" package for "R". sample dataset looks like this:
> data_2[510:520,]
lon lat class
510 -47.61849 40.00805 2
511 -47.36740 40.01180 1
512 -47.11629 40.01551 1
513 -46.86518 40.01918 1
514 -46.61404 40.02282 1
515 -46.36290 40.02642 3
516 -46.11173 40.02999 1
517 -45.86056 40.03352 1
518 -45.60937 40.03700 3
519 -45.35817 40.04045 3
520 -45.10695 40.04386 3
The longitude and latitude values in the main dataset are not continuous.
My problem is I do not have "class" value for all the latitude-longitude combinations, and due to this there are lot of empty spaces left when i am trying to plot the above values. What I want is to get a continuous, filled (for all lat-long combinations) plot.
The following is an example of one of the ways i am trying to plot:
levelplot(data_2$class ~ data_2$lon * data_2$lat, data = data_2, region = TRUE, aspect = "fill")
Are there any options available in the levelp开发者_运维问答lot() or contourplot() functions which i can use to achieve this or is there any other package/method in "R" which could help me come up with this solution?
I recommend taking a look at the free ebook "A Practical Guide to Geostatistical Mapping" (http://spatial-analyst.net/book/download) for a review of spatial estimation methods with plenty of examples in R.
As Ben pointed out, you'll need to do some sort of spatial interpolation. Here's a quick example using the interpolate
function in the intamap
package:
library(intamap)
library(lattice)
# Generate an example dataset
set.seed(10)
class1 <- data.frame(lon=rnorm(50, mean=-46, sd=4),
lat=rnorm(50, mean=32, sd=4),
value=1)
class2 <- data.frame(lon=rnorm(50, mean=-40, sd=4),
lat=rnorm(50, mean=39, sd=4),
value=2)
class3 <- data.frame(lon=rnorm(50, mean=-50, sd=3),
lat=rnorm(50, mean=40, sd=2),
value=3)
df <- rbind(class1, class2, class3)
# Generate a 50 x 50 grid over which to predict new values
prediction.grid <- expand.grid(lon=seq(from=min(df$lon),
to=max(df$lon),
length=50),
lat=seq(from=min(df$lat),
to=max(df$lat),
length=50))
# Spatialize the data.frames
coordinates(df) <- c("lon", "lat")
gridded(prediction.grid) <- c("lon", "lat")
fit <- interpolate(df, prediction.grid)
# Built-in plots, including variogram and pertinent stats:
plot(fit)
# Pull out the fitted values into a dataframe
predictions <- as.data.frame(t(fit$outputTable))
levelplot(mean ~ x * y, data=predictions, region=TRUE, aspect="fill")
You need to do some kind of interpolation first. The akima
package is probably your best bet, see the examples in ?akima
. gstat::krige
is another possibility.
精彩评论