重组 sf multi-plot 并添加图例

Reorganize sf multi-plot and add a legend

我正在尝试使用 sf::plot 并排绘制两张地图,但我无法让它工作。有两个问题,第一个是情节是相互重叠而不是并排的,第二个是我失去了传说。

这里有一个例子和更多的解释。

library(sf) 
library(dplyr)

# preparing the shapefile
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  select(AREA, PERIMETER) %>% 
  mutate(AREA = as.factor(AREA<median(AREA)))

如果我独立绘制每个字段:

plot(nc[,1])

plot(nc[,2])

两张图片都不错,都有图例和所有内容,但我希望它们都在同一个面板上。 sf::plot 提供此内置功能,如 https://r-spatial.github.io/sf/articles/sf5.html#geometry-with-attributes-sf:

中所述
plot(nc)

我失去了传说,他们是在彼此之上而不是并排。在 ?plot 您可以阅读:

For more control over individual maps, set parameter mfrow with par prior to plotting, and plot single maps one by one.

但是当我这样做时,它不起作用:

par(mfrow=c(1,2))
plot(nc[,1])
plot(nc[,2])
par(mfrow=c(1,1))

知道如何用 sf 并排绘制 2 张地图吗?

我没有在 sf 包中找到解决方案。我发现这个可能适合你

library(ggplot2)
area<-ggplot() + geom_sf(data = nc[,1], aes(fill = AREA))
perim<-ggplot() + geom_sf(data = nc[,2], aes(fill = PERIMETER))
gridExtra::grid.arrange(area,perim,nrow=1)

最后还是文档的问题。为了能够将 parsf::plot 一起使用,您需要执行以下任一操作:

par(mfrow=c(1,2))
plot(st_geometry(nc[,1]))
plot(st_geometry(nc[,2]))
par(mfrow=c(1,1))

par(mfrow=c(1,2))
 plot(nc[,1], key.pos = NULL, reset = FALSE)
 plot(nc[,2], key.pos = NULL, reset = FALSE)
par(mfrow=c(1,1))

但是,您在第一种情况下丢失了颜色,在这两种情况下都丢失了图例。您必须自己手动管理它。

参见:https://github.com/r-spatial/sf/issues/877

要添加到@Bastien 的答案,您可以手动添加图例。这是一个使用 leaflet and plotrix 库添加连续图例的简单函数:

addLegendToSFPlot <- function(values = c(0, 1), labels = c("Low", "High"), 
                              palette = c("blue", "red"), ...){

    # Get the axis limits and calculate size
    axisLimits <- par()$usr
    xLength <- axisLimits[2] - axisLimits[1]
    yLength <- axisLimits[4] - axisLimits[3]

    # Define the colour palette
    colourPalette <- leaflet::colorNumeric(palette, range(values))

    # Add the legend
    plotrix::color.legend(xl=axisLimits[2] - 0.1*xLength, xr=axisLimits[2],
                          yb=axisLimits[3], yt=axisLimits[3] + 0.1 * yLength,
                          legend = labels, rect.col = colourPalette(values), 
                          gradient="y", ...)
}

通过@Bastien 的代码使用上述函数:

# Load required libraries
library(sf) # Working spatial data
library(dplyr) # Processing data
library(leaflet) # Has neat colour palette function
library(plotrix) # Adding sf like legend to plot

# Get and set plotting window dimensions
mfrowCurrent <- par()$mfrow
par(mfrow=c(1,2))

# Add sf plot with legend
plot(nc[,1], key.pos = NULL, reset = FALSE)
addLegendToSFPlot(values = c(0, 1), 
                  labels = c("False", "True"),
                  palette = c("lightseagreen", "orange"))

# Add sf plot with legend
plot(nc[,2], key.pos = NULL, reset = FALSE)
valueRange <- range(nc[, 2, drop = TRUE])
addLegendToSFPlot(values = seq(from = valueRange[1], to = valueRange[2], length.out = 5), 
                  labels = c("Low", "", "Medium", "", "High"),
                  palette = c("blue", "purple", "red", "yellow"))

# Reset plotting window dimensions
par(mfrow=mfrowCurrent)