我有一个放在一起的自定义形状文件。当我一次绘制所有内容时,效果很好。但我想按某些变量进行分组来绘制特定形状的区域。例如:
+-------------+--------+-------+
| county | region | sales |
+-------------+--------+-------+
| washoe | 1 | 5 |
| carson city | 1 | 10 |
| clark | 2 | 15 |
| harmon | 2 | 20 |
+-------------+--------+-------+
如果我运行:
leaflet() %>% addTiles %>% addPolygons(data=df)
这将根据这四个多边形的位置绘制四个单独的多边形。
但假设我想根据区域创建多边形。因此从理论上讲,输出将仅包含两个多边形。一个是蒙哥马利和史蒂文斯的组合形状,另一个是摩西和哈蒙县的组合形状,我还想总结一下总销售额。
x <- df %>% group_by(region) %>% summarise(totals=sum(ones))
它会给我这个错误:
Error in CPL_geos_union(st_geometry(x), by_feature) :
Evaluation error: TopologyException: Input geom 0 is invalid: Ring Self-intersection at or near point -119.97497995771261 39.521600907266631 at -119.97497995771261 39.521600907266631
就几何变量的信息而言:
df$geometry
Geometry set for 2106 features
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -115.8968 ymin: 35.00184 xmax: -114.0428 ymax: 36.85366
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
我需要将其转换为其他东西吗?
使用 OP 进行编辑data https://github.com/gooponyagrinch/custom_shape:
shp <- st_read("file/path/sample_shape.shp")
shp_union <- shp %>%
mutate(turf = as.character(turf),
turf = ifelse(is.na(turf), "NA", turf)) %>%
group_by(turf) %>%
summarize(geometry = st_union(geometry),
total_sales = sum(sales))
原始几何形状
ggplot() + geom_sf(data = shp, aes(fill = sales))
新的几何形状
ggplot() + geom_sf(data = shp_union, aes(fill = total_sales))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)