sf 点对象图:固定边框颜色和不同背景颜色

sf point object plot: fixed border color and different background colors

我很难绘制 sf 点对象。 基本上我想根据变量显示带有黑色边框和填充颜色的点。 在基本图中,我会这样做以使用不同的背景颜色施加边框颜色:

plot(1:3, cex = 3, pch = 21, col = 'red', bg = c('blue', 'green', 'yellow'), lwd = 2)

我使用 pch = 21 来用 bg 颜色填充点,同时使用 col = 'red' 作为点边框颜色。

使用 sf 对象我可以使用 col 来施加边框颜色但是图例消失了(根据 plot {sf} 的文档,如果 col 被传递到图例被抑制的功能)。

library(sf)
# example
df <- data.frame(est = c('A', 'B', 'C'), 
             long = c(-70, -68, -69), 
             lat = c(-12.2, -12.4, -12), 
             RMSE = c(25, 8, 55)) %>% 
    st_as_sf(coords = c('long', 'lat'), crs = 4326)

# reclassifying the RMSE variable:
df$rmse_cut <- cut(df$RMSE, c(0,15,30,45,60))

# plotting the map
plot(df['rmse_cut'], main = 'RMSE', graticule = TRUE, axes = TRUE, 
bgc = 'gray92', 
pch = 21, lwd = 2, cex = 3,
col = 'black', 
bg = c('blue', 'green', 'yellow'))

但在没有 col 的情况下绘图:

plot(df['rmse_cut'], main = 'RMSE', graticule = TRUE, axes = TRUE, 
bgc = 'gray92', 
pch = 21, lwd = 2, cex = 3,
bg = c('blue', 'green', 'yellow'))

边框按变量rmse_cut排序,但背景颜色是固定的。 如何在使用背景颜色显示 rmse_cut 变量的不同值时对点边框使用单一颜色?

忘记基本绘图,geom_sf()ggplot2 的最新 CRAN 版本中。

library(ggplot2)

ggplot(df, aes(fill = rmse_cut)) +
  geom_sf(size = 5, color = "black", shape = 21, stroke = 2) +
  coord_sf(ylim = c(-11,-13))

由于您的 lat 值太紧,我手动扩大了比例。

记住基地密谋!你需要整理你的调色板,使它们一致,然后调用值引用的调色板:

plotpal <- sf.colors(nlevels(df[['rmse_cut']]),categorical=TRUE)
plot(df['rmse_cut'], main = 'RMSE', graticule = TRUE, axes = TRUE, bgc = 'gray92',
     type="n", pal=plotpal, key.pos=1, reset=FALSE)
plot(df[['geometry']], pch = 21, lwd = 2, cex = 3, bg=plotpal[df[['rmse_cut']]], add=TRUE)