r - 在 .filled.contour 中设置自动颜色级别? (克里格法)

标签 r plot kriging spdf

我很难使用显示克里金图的filled.contour自动创建颜色,避免指定levels()。

我可以绘制结果并查看图例,但为什么我的颜色是重复的?因此,为什么区间 4.5 - 5.0 的颜色与 7.0 - 7.5 的颜色相同?以及如何修复它?

filled.contour(x = seq(0,1, length.out = nrow(predmat3)),
           y = seq(0,1, length.out = ncol(predmat3)),
           z = predmat3,
           col = brewer.pal(5,"Purples"), nlevels = 5)

enter image description here

相当长的可重现示例,改编自 https://rpubs.com/nabilabd/118172 (通过对插值点的 voronoi 分割数据进行采样来计算克里金法)

library(sp)
library(gstat)
library(RColorBrewer)

# https://rpubs.com/nabilabd/118172
# packages for manipulation & visualization
suppressPackageStartupMessages({
  library(dplyr) # for "glimpse"
  library(ggplot2)
  library(scales) # for "comma"
  library(magrittr)
})

data(meuse)

# create spdf
meuse.spdf<-meuse

# convert to spdf
coordinates(meuse.spdf) <- ~ x + y



# calculate voronoi tesselation - will be needed to create underlying point data
voronoipolygons = function(layer) {
  require(deldir)
  crds = layer@coords
  z = deldir(crds[,1], crds[,2])
  w = tile.list(z)
  polys = vector(mode='list', length=length(w))
  require(sp)
  for (i in seq(along=polys)) {
    pcrds = cbind(w[[i]]$x, w[[i]]$y)
    pcrds = rbind(pcrds, pcrds[1,])
    polys[[i]] = Polygons(list(Polygon(pcrds)), ID=as.character(i))
  }
  SP = SpatialPolygons(polys)
  voronoi = SpatialPolygonsDataFrame(SP, data=data.frame(dummy = seq(length(SP)), row.names=sapply(slot(SP, 'polygons'), 
                                                                                                   function(x) slot(x, 'ID'))))
}
meuse.voro <- voronoipolygons(meuse.spdf)   

# create underlying grid
s.grid <- spsample(meuse.voro, type = "regular", n = 6000)

# calculate kriging
 # create variogram
lzn.vgm <- variogram(log(zinc)~1, meuse.spdf) # calculates sample variogram values 
lzn.fit <- fit.variogram(lzn.vgm, model=vgm(1, "Sph", 900, 1)) # fit model

plot(lzn.vgm, lzn.fit) # plot the sample values, along with the fit model

# calculate kriging
lzn.kriged <- krige(log(zinc) ~ 1, meuse.spdf, s.grid, model=lzn.fit)


# extract the unique x and y locations in the grid
ux<-unique(coordinates(lzn.kriged)[,1])
uy<-unique(coordinates(lzn.kriged)[,2])

# extract the predicted values and format var1.pred into a matrix of gridded values
predmat3 <- matrix(lzn.kriged$var1.pred, length(ux), length(uy))

# display the data???

filled.contour(x = seq(0,1, length.out = nrow(predmat3)),
           y = seq(0,1, length.out = ncol(predmat3)),
           z = predmat3,
           col = brewer.pal(5,"Purples"), nlevels = 5)

最佳答案

您需要 6 种颜色作为图例,但只指定了 5 种,因此只需更改为:

filled.contour(x = seq(0,1, length.out = nrow(predmat3)),
               y = seq(0,1, length.out = ncol(predmat3)),
               z = predmat3,
               col = brewer.pal(6,"Purples"), nlevels = 5)

enter image description here

关于r - 在 .filled.contour 中设置自动颜色级别? (克里格法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177017/

相关文章:

R:无法安装 AzureML 包(R 版本 3.3.3)

r - 将向量列表转换为数据框

javascript - R 中 Shiny 的 STRING 交互网络

python - 从最佳拟合线找出斜率趋势

kriging - 使用高斯过程回归或克里金法进行外推

r - 将 purrr 和 ggplot 与 group_by 和 Nest() 结合使用时获取绘图标题

r - 正态分布均值贝叶斯推理的玩具 R 代码 [降雪量数据]

r - 将 R 绘图 x 轴设置为在 y=0 处显示

r - 如何在 R 中计算变差函数时更改滞后距离