12
library(reshape2)
library(data.table)
library(dplyr)
library(magrittr)
library(ggplot2)
library(scales)
library(gstat)
library(DescTools)
library(sp)



#I want a colorado grid#
data("colorado.grid")

#making cordinates into spatial points dataframe#
coordinates(Gold_tracer_kri) <- ~ long_orig + lat_orig
#attempt at kriging but no grid#
lzn.kriged <- krige(Au ~ 1, Gold_tracer_kri, colorado.grid, model=lzn.fit)

lzn.kriged %>% as.data.frame %>%
ggplot(aes(long_orig=long_orig, lat_orig=lat_orig)) +     geom_tile(aes(fill=var1.pred)) + coord_equal() +
scale_fill_gradient(low = "yellow", high="red") +
scale_x_continuous(labels=comma) + scale_y_continuous(labels=comma) +
theme_bw()

加载空间域进行插值

data("meuse.grid")

我正在尝试在 R 中使用克里金方法,但由于无法为我的数据找到网格而被卡住了。我的数据占据了美国科罗拉多州的所有地区,我希望能够获得一个网格来插入我的数据。类似于我正在关注的示例中使用的 meuse.grid。

任何帮助,将不胜感激

4

1 回答 1

21

使用方法sp

请参阅sf下面的版本!

您可以使用创建网格sp::makegrid

library(sp)
library(rgdal)
library(raster)

# load some spatial data. Administrative Boundary
us <- getData('GADM', country = 'US', level = 1)
us$NAME_1
colorado <- us[us$NAME_1 == "Colorado",]


# check the CRS to know which map units are used
proj4string(colorado)
# "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

# Create a grid of points within the bbox of the SpatialPolygonsDataFrame 
# colorado with decimal degrees as map units
grid <- makegrid(colorado, cellsize = 0.1) # cellsize in map units!

# grid is a data.frame. To change it to a spatial data set we have to
grid <- SpatialPoints(grid, proj4string = CRS(proj4string(colorado)))

plot(colorado)
plot(grid, pch = ".", add = T)

以科罗拉多为例

另一个例子是奥地利(GADM 代码'AUT')。 以奥地利为例

仅在多边形内的网格点:

要仅提取多边形内的点,请使用`[`基于位置的点子集,如下所示:

grid <- grid[colorado, ]

使用方法sf

library(sf)
library(raster)
library(ggplot2)

# load some spatial data. Administrative Boundary
aut <- getData('GADM', country = 'aut', level = 0)
aut <- st_as_sf(aut)
# ggplot() + 
  # geom_sf(data = aut)

grid <- aut %>% 
  st_make_grid(cellsize = 0.1, what = "centers") %>% # grid of points
  st_intersection(aut)                               # only within the polygon

# ggplot() + 
  # geom_sf(data = aut) + 
  # geom_sf(data = grid)
于 2017-01-22T09:57:55.403 回答