首页 > 解决方案 > 特定多面体上的 R 密度图

问题描述

我想用下面的代码创建一个热图。

data_smoothed = ggplot() +
  metR::geom_contour_fill(data = data.dolphins, aes(x = lon, y = lat, z = density), 
                          bins = 120, na.fill = TRUE)+
  coord_sf(xlim = c(-1, 5), ylim = c(37,43))+ #I want to change this 
  scale_fill_viridis_c() +
  metR::scale_x_longitude(ticks = 2)+
  metR::scale_y_latitude(ticks=2)+
  theme_bw() +
  ggtitle("dolphins")+
  theme(plot.title = element_text(hjust = 0.5)) 

它返回这个: 在此处输入图像描述

但是,我不想收到这个,我只想保留热图的这些部分,这些部分属于我已经拥有的多面体。

> poly
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -1 ymin: 37 xmax: 6 ymax: 42.42764
Geodetic CRS:  WGS 84
                        geometry
1 MULTIPOLYGON (((0.5 39.5, 0...

即类似于下面显示的多边形内的热图部分。 在此处输入图像描述

有任何想法吗?

标签: rggplot2heatmapkernel-density

解决方案


似乎这个线程在多边形区域内做了类似的 ggplot2/gis 绘图 我过去也使用 sf 包中的 st_intersection() 做过类似的事情,在绘图之前过滤与多边形重叠的数据。


推荐阅读