3
votes

I have a data set that contains x and y coordinates of multiple polygons, such as

df <- data.frame(
  xpol = c(0.304147897, 0.272762377, 0.239435395, 0.204166952, 0.166957048, 0.127805683, 0.086712856, 0.043678568, -0.001297181, -0.048214391, -0.097073063, -0.147873196, -0.20061479, -0.255297845, -0.311922362, -0.37048834, -0.430995779, -0.493444679, -0.557835041, -0.624166864, -0.692440148, -0.698421984, -0.629639016, -0.562974508, -0.49842846, -0.436000872, -0.375691745, -0.317501078, -0.261428871, -0.207475125, -0.155639838, -0.105923012, -0.058324646, -0.012844741, 0.030516705, 0.07175969, 0.110884215, 0.147890279, 0.182777884, 0.215547028, 0.246197712, 0.274729935, 0.460208646, 0.414820816, 0.370699056, 0.327843365, 0.286253743, 0.245930191, 0.206872708, 0.169081295, 0.132555951, 0.097296676, 0.063303471, 0.030576335, -0.000884731, -0.031079728, -0.060008655, -0.087671513, -0.114068302, -0.139199021, -0.163063671, -0.185662251, -0.206994762, -0.236134298, -0.212015307, -0.186885196, -0.160743963, -0.133591608, -0.105428133, -0.076253536, -0.046067818, -0.014870978, 0.017336983, 0.050556065, 0.084786268, 0.120027593, 0.156280039, 0.193543606, 0.231818295, 0.271104105, 0.311401036, 0.352709088, 0.395028262, 0.438358557, 0.209348542, 0.187353371, 0.163237565, 0.137001125, 0.108644051, 0.078166342, 0.045567999, 0.010849022, -0.02599059, -0.064950835, -0.106031716, -0.14923323, -0.194555379, -0.241998162, -0.29156158, -0.343245631, -0.397050318, -0.452975638, -0.511021593, -0.571188182, -0.633475405, -0.640474396, -0.577641289, -0.517236848, -0.459261072, -0.403713963, -0.350595518, -0.29990574, -0.251644627, -0.20581218, -0.162408398, -0.121433282, -0.082886832, -0.046769047, -0.013079928, 0.018180525, 0.047012313, 0.073415435, 0.097389891, 0.118935682, 0.138052807, 0.154741267, 0.553882586, 0.49867869, 0.443843518, 0.38937707, 0.335279346, 0.281550347, 0.228190072, 0.175198522, 0.122575695, 0.070321594, 0.018436216, -0.033080437, -0.084228366, -0.135007571, -0.185418051, -0.235459807, -0.285132839, -0.334437146, -0.383372729, -0.431939588, -0.480137722, -0.492000258, -0.442687027, -0.393147385, -0.343381332, -0.293388868, -0.243169994, -0.192724709, -0.142053012, -0.091154906, -0.040030388, 0.011320541, 0.06289788, 0.11470163, 0.166731791, 0.218988363, 0.271471345, 0.324180739, 0.377116543, 0.430278758, 0.483667384, 0.53728242, 0.242356084, 0.217556549, 0.191629161, 0.164573921, 0.136390827, 0.10707988, 0.076641081, 0.045074429, 0.012379923, -0.021442435, -0.056392646, -0.09247071, -0.129676627, -0.168010397, -0.207472019, -0.248061495, -0.289778824, -0.332624005, -0.37659704, -0.421697927, -0.467926667, -0.480137722, -0.432800923, -0.386818069, -0.342189162, -0.2989142, -0.256993185, -0.216426115, -0.177212991, -0.139353813, -0.102848581, -0.067697295, -0.033899955, -0.00145656, 0.029632888, 0.059368391, 0.087749947, 0.114777558, 0.140451223, 0.164770942, 0.187736715, 0.209348542),
  ypol = c(-0.96024812, -0.864762269, -0.770354343, -0.677024339, -0.584772259, -0.493598102, -0.403501868, -0.314483558, -0.226543172, -0.139680708, -0.053896168, 0.030810448, 0.114439142, 0.196989911, 0.278462758, 0.358857681, 0.438174681, 0.516413757, 0.593574911, 0.66965814, 0.744663447, 0.739258298, 0.664759825, 0.589116066, 0.512327021, 0.43439269, 0.355313072, 0.275088169, 0.193717979, 0.111202503, 0.027541741, -0.057264307, -0.143215641, -0.230312262, -0.318554169, -0.407941361, -0.49847384, -0.590151605, -0.682974656, -0.776942994, -0.872056617, -0.968315527, -0.900329625, -0.810089238, -0.719434002, -0.628363916, -0.53687898, -0.444979196, -0.352664561, -0.259935078, -0.166790745, -0.073231563, 0.020742469, 0.11513135, 0.20993508, 0.30515366, 0.400787089, 0.496835367, 0.593298495, 0.690176472, 0.787469299, 0.885176975, 0.9832995, 0.977457062, 0.879878578, 0.782634537, 0.68572494, 0.589149786, 0.492909076, 0.39700281, 0.301430987, 0.206193608, 0.111290672, 0.01672218, -0.077511868, -0.171411473, -0.264976634, -0.358207351, -0.451103625, -0.543665456, -0.635892842, -0.727785785, -0.819344285, -0.910568341, -0.982860934, -0.885048061, -0.788181629, -0.692261638, -0.597288088, -0.503260979, -0.410180312, -0.318046085, -0.226858299, -0.136616954, -0.04732205, 0.041026412, 0.128428434, 0.214884015, 0.300393155, 0.384955854, 0.468572111, 0.551241928, 0.632965304, 0.713742239, 0.793572732, 0.788156881, 0.708832715, 0.628491594, 0.547133519, 0.464758488, 0.381366503, 0.296957562, 0.211531666, 0.125088816, 0.03762901, -0.050847751, -0.140341466, -0.230852137, -0.322379763, -0.414924343, -0.508485879, -0.60306437, -0.698659815, -0.795272216, -0.892901572, -0.991547883, -0.848695004, -0.763721189, -0.678538743, -0.593147667, -0.50754796, -0.421739624, -0.335722657, -0.24949706, -0.163062833, -0.076419975, 0.010431513, 0.097491631, 0.184760379, 0.272237757, 0.359923766, 0.447818405, 0.535921675, 0.624233574, 0.712754104, 0.801483264, 0.890421054, 0.884257179, 0.795895062, 0.707660145, 0.619552428, 0.531571913, 0.443718598, 0.355992484, 0.26839357, 0.180921857, 0.093577345, 0.006360033, -0.080730078, -0.167692989, -0.254528698, -0.341237207, -0.427818516, -0.514272624, -0.600599531, -0.686799237, -0.772871743, -0.858817048, -0.976092379, -0.878681978, -0.781669249, -0.685054193, -0.588836809, -0.493017098, -0.39759506, -0.302570694, -0.207944002, -0.113714981, -0.019883634, 0.073550041, 0.166586043, 0.259224373, 0.351465029, 0.443308014, 0.534753325, 0.625800964, 0.71645093, 0.806703223, 0.896557844, 0.890421054, 0.801147849, 0.711412445, 0.621214841, 0.530555037, 0.439433035, 0.347848833, 0.255802431, 0.16329383, 0.07032303, -0.02310997, -0.117005169, -0.211362567, -0.306182165, -0.401463963, -0.497207959, -0.593414155, -0.690082551, -0.787213146, -0.88480594, -0.982860934),
  ID = c(rep(849, 42), rep(6249, 42), rep(7809, 42), rep(17045, 42), rep(18495, 42))
)

which one can plot with

ggplot() +
  geom_polygon(aes(x = xpol, y = ypol, group = factor(ID)), 
               data = df, 
               fill = "grey", color = "black", alpha = 0.6) +
  theme_bw() +
  xlim(-1,1) +
  ylim(-1,1)

original plot

As one can see, some polygons overlap...

I would like to merge the data such as the polygons that overlap are transformed into a single polygon, so the resulting plot would be (graphic manually edited)

modified plot

For completeness, a polygon might overlap with none, 1, or many (many many) other polygons.

1

1 Answers

5
votes

Something like this, perhaps?

library(dplyr)
library(sf)

sf.object <- df %>%
  # convert data frame to sf object
  st_as_sf(coords = c("xpol", "ypol")) %>%

  # cast points associated with each ID as a polygon
  group_by(ID) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON") %>%
  ungroup() %>%

  # union polygons
  st_union()

# plot result using geom_sf()
ggplot() +
  geom_sf(data = sf.object,
          fill = "grey", color = "black", alpha = 0.6) +
  theme_bw() +
  coord_sf(xlim = c(-1, 1),
           ylim = c(-1, 1))

plot