Séb*_*tte 10 r polygon geospatial ggplot2 broom
我想SpatialPolygons
从sp
ggplot2中带孔的库中绘制.感谢stackoverflow上的其他问题,我知道在处理顺时针写入的多边形时允许这样做:
http://stackoverflow.com/questions/12047643/geom-polygon-with-multiple-hole/12051278#12051278
确实,在转换时SpatialPolygons
使用broom::tidy
(替换ggplot2::fortify
),孔多边形以顺时针方向保存,以绘制为孔.
在ggplot2中,绘制带有孔的多边形的方式强制一次使用它们fill
,另一次使用colour
,否则你可能会看到穿过多边形的线条.当处理多个子多边形时,一些有孔,这更棘手,定义的点特征的顺序broom::tidy
可能不允许填充多边形(见下图).
你们有没有解决方案摆脱这种填充问题的行为?
这是一个可重复的例子:
library(sp)
library(ggplot2)
# Create two polygons: second would be a hole inside the first
xy = cbind(
x = c(13.4, 13.4, 13.6, 13.6, 13.4),
y = c(48.9, 49, 49, 48.9, 48.9)
)
hole.xy <- cbind(
x = c(13.5, 13.5, 13.45, 13.45, 13.5),
y = c(48.98, 48.92, 48.92, 48.98, 48.98)
)
# Transform as SpatialPolygons with holes
xy.sp <- SpatialPolygons(list(
Polygons(list(Polygon(xy),
Polygon(hole.xy, hole = TRUE)), "1"),
Polygons(list(Polygon(xy + 0.2),
Polygon(xy + 0.35),
Polygon(hole.xy + 0.2, hole = TRUE)), "2")
))
# Transform SpatialObject to be used by ggplot2
xy.sp.l <- broom::tidy(xy.sp)
ggplot(xy.sp.l) +
geom_polygon(aes(x = long, y = lat, group = id, fill = id))
Run Code Online (Sandbox Code Playgroud)
lbu*_*ett 11
可能是"过去" sf
包装的好时机.由于几何形状,使用sf
对象实际上更容易:ggplot
geom_sf
library("sf")
library("rgeos")
sf_poly <- as(xy.sp, "sf")
sf::st_crs(sf_poly) <- 4326
sf_poly$id <- c(1,2)
ggplot(sf_poly) +
geom_sf(aes(fill = as.factor(id)))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
771 次 |
最近记录: |