R 围绕光栅单元打印轮廓

R 围绕光栅单元打印轮廓,r,ggplot2,R,Ggplot2,我想使用ggplot2绘制一个多边形(凸包?),该多边形尊重光栅图(使用geom\u光栅创建)的单元边界。我可以使用凸包方法,但它们将通过x-y坐标,而不是光栅图的单元格(对不起,我的术语受我的无知影响)。例如,给定以下具有分类属性d的x-y坐标数据: df <- data.frame(x = c(295, 300, 305, 310, 295, 300, 305, 310), y = c(310, 310, 310, 310, 315, 315, 315

我想使用ggplot2绘制一个多边形(凸包?),该多边形尊重光栅图(使用
geom\u光栅创建)的单元边界。我可以使用凸包方法,但它们将通过x-y坐标,而不是光栅图的单元格(对不起,我的术语受我的无知影响)。例如,给定以下具有分类属性
d
的x-y坐标数据:

df <- data.frame(x = c(295, 300, 305, 310, 295, 300, 305, 310),
                 y = c(310, 310, 310, 310, 315, 315, 315, 315),
                 d = c(2, 2, 2, 1, 2, 1, 1, 1))
给予:

但我想要的是由
d
定义的类别的概要外壳。大概是这样的:


我可以用base R和ggplot2来实现这一点吗?或者有没有办法使用
光栅
软件包或其他网格/光栅/GIS软件包?我更喜欢让它尽可能简单。

你当然可以使用R的空间设施来获得这些多边形。这里有一个方法:

library(raster)

## Convert your data.frame to a raster object
r <- rasterFromXYZ(df)

## Extract polygons
pp <- rasterToPolygons(r, dissolve=TRUE)

## Convert SpatialPolygons to a format usable by ggplot2
outline <- fortify(pp)

## Put it all together:
ggplot(df, aes(x, y)) +
    geom_raster(aes(fill = d)) +
    coord_fixed() +
    geom_path(aes(x = long, y = lat, group = group), data = outline, 
              size=1.5, col="gold")
库(光栅)
##将data.frame转换为光栅对象

r不幸的是,{ggplot2}中的
fortify()
不再被包作者推荐。那些包作者也不再推荐使用{broom}包中的建议替换项
tidy()

幸运的是,我们可以使用{sf}包重新创建Josh O'Brien的答案。我们仍然依赖于{raster}包中的一些函数,但是一旦假定的错误被修复,这些函数可能会被{stars}包替换(请参阅)。如果光栅非常大,使用{stars}可能会非常有利,因为将光栅(或数据帧)转换为连续多边形可能非常慢(请参见)

下面是代码(借用Josh O'Brien的设置):


这很有效,但对我来说只是其中的一部分。有没有办法将多边形对象用作ggplot2中的图层?也许是另一个光栅包转换例程?@AlexT尝试,例如,做
obj,效果很好。我似乎必须加载maptools包才能使fortify正常工作。我可以将整个转换组合在一个dplyr字符串中,这使它变得美观易读:
outline%rasterFromXYZ()%%>%rasterToPolygons(dissole=TRUE)%%>%fortify(region=“d”)
感谢您使用
fortify
步骤更新您的答案。
library(raster)

## Convert your data.frame to a raster object
r <- rasterFromXYZ(df)

## Extract polygons
pp <- rasterToPolygons(r, dissolve=TRUE)

## Convert SpatialPolygons to a format usable by ggplot2
outline <- fortify(pp)

## Put it all together:
ggplot(df, aes(x, y)) +
    geom_raster(aes(fill = d)) +
    coord_fixed() +
    geom_path(aes(x = long, y = lat, group = group), data = outline, 
              size=1.5, col="gold")
library(raster)
library(rgeos)
library(ggplot2)
library(sf)

df <- data.frame(x = c(295, 300, 305, 310, 295, 300, 305, 310),
                 y = c(310, 310, 310, 310, 315, 315, 315, 315),
                 d = c(2, 2, 2, 1, 2, 1, 1, 1))

## Convert your data.frame to a raster object
r <- raster::rasterFromXYZ(df)

## Extract polygons
pp <- raster::rasterToPolygons(r, dissolve = TRUE)

## Convert SpatialPolygons to a format usable by ggplot2
outline <- sf::st_as_sf(pp)

## Put it all together:
## The polygon "outline" is filled by default so will cover up the
## raster values. Use fill = NA for the geom_sf() to just use the
## gold border color. 
ggplot(df) +
  geom_raster(aes(x = x, y = y, fill = d)) +
  geom_sf(data = outline, size = 1.5, col = "gold", fill = NA)
library(raster)
library(rgeos)
library(ggplot2)
library(sf)

df <- data.frame(x = c(295, 300, 305, 310, 295, 300, 305, 310),
                 y = c(310, 310, 310, 310, 315, 315, 315, 315),
                 d = c(2, 2, 2, 1, 2, 1, 1, 1))

## Convert your data.frame to a raster object
r <- raster::rasterFromXYZ(df)

## Extract polygons
pp <- raster::rasterToPolygons(r, dissolve = TRUE)

## Or you can cast to a linestring instead of using a polygon
## and then having to use the fill = NA argument in the 
## geom_sf() call
outline <- sf::st_as_sf(pp) %>% st_cast("LINESTRING")

ggplot(df) +
  geom_raster(aes(x = x, y = y, fill = d)) +
  geom_sf(data = outline, size = 1.5, col = "gold")