R缓冲/放大多边形

时间:2018-09-09 12:25:44

标签: r

我有一个简单的多边形。

dfr <- data.frame(x=c(2,2.5,4,5,4.5,3,2),y=c(2,3,3.5,3,2.8,2.5,2))
plot(dfr)
polygon(dfr)

enter image description here

是否有R函数可以在各个方向上均等地增加多边形的大小?

enter image description here

2 个答案:

答案 0 :(得分:1)

使用sf包,可以将多边形转换为空间对象并使用st_buffer

> p = st_polygon(list(as.matrix(dfr)))
> pbuf = st_buffer(p, .4)
> plot(pbuf)
> plot(p,add=TRUE,col="red")
> 

enter image description here

答案 1 :(得分:0)

出于纯粹的绘图目的,我找到了这个解决方案。

library(ggplot2)
dfr <- data.frame(x=c(2,2.5,4,5,4.5,3,2),y=c(2,3,3.5,3,2.8,2.5,2))

# vanilla polygon
ggplot(dfr,aes(x,y))+
  geom_polygon(fill=NA,col="black")+
  theme_minimal()

enter image description here

# enlarge polygon
library(ggforce)
ggplot(dfr,aes(x,y))+
  geom_polygon(fill=NA,col="black")+
  geom_shape(fill=NA,col="red",expand=unit(0.2,"cm"))+
  theme_minimal()

enter image description here

# enlarge with pretty curved edges
library(ggforce)
ggplot(dfr,aes(x,y))+
  geom_polygon(fill=NA,col="black")+
  geom_shape(fill=NA,col="red",expand=unit(0.2,"cm"),radius=unit(0.2,"cm"))+
  theme_minimal()

enter image description here

请注意,CRAN上的ggforce 0.1.3版本尚不具备此功能。 GitHub上的版本具有此功能。

相关问题