2

I'm trying to replace my multipolygons from my tibble with a geometry object into points. I'm using the sf package for plotting. It's hard to give a reproducable sample because of the many datapoints in geometry.

this: From the sf vignettes, Miscellaneous

into this: From the sf vignettes, Miscellaneous

But I don't use base R but ggplot2 and my object is a tibble with a geometry variable. Here's my code:

library(tidyverse)
library(sf)

utrecht_sf %>%
  ggplot() +
  geom_sf(aes(fill = partij),
          size = 0.4,
          colour = "white") +
  coord_sf(datum = NA) +
  scale_fill_colorblind() +
  theme_minimal()

utrecht_sf

# A tibble: 43 x 12
   stad    postcode partij     stemmen totaal_stemmen perct_grootste_van_totaal OBJECTID Aantal_mul Aantal_adr Shape_Leng Shape_Area                      geometry
   <chr>   <chr>    <chr>        <int>          <int>                     <int>    <dbl>      <dbl>      <dbl>      <dbl>      <dbl>             <sf_geometry [m]>
 1 Utrecht 3451     VVD           1612           6598                        24     1074       1.00       4548      24960    7787919 MULTIPOLYGON (((127829.1 45.…
 2 Utrecht 3452     VVD           1038           4013                        26     1075       1.00       5239       8278    1938913 MULTIPOLYGON (((128499.4 45.…
 3 Utrecht 3453     VVD            784           3135                        25     1076       1.00       3422       8089    1617391 MULTIPOLYGON (((128803.5 45.…
 4 Utrecht 3454     VVD            985           4609                        21     1077       1.00       5591      31103    8025353 MULTIPOLYGON (((130358.6 45.…
 5 Utrecht 3455     VVD             60            260                        23     1078       1.00        202      16097    6880581 MULTIPOLYGON (((128011.3 46.…
 6 Utrecht 3511     GROENLINKS    2948           9840                        30     1087       1.00       6951       6808    1222881 MULTIPOLYGON (((136430.8 45.…
 7 Utrecht 3512     GROENLINKS    1905           6528                        29     1088       1.00       5454       5817     852599 MULTIPOLYGON (((137189.5 45.…
 8 Utrecht 3513     GROENLINKS     746           2717                        27     1089       1.00       3550       3294     517187 MULTIPOLYGON (((136097 4571.…
 9 Utrecht 3514     GROENLINKS     882           2948                        30     1090       1.00       3659       4900     590764 MULTIPOLYGON (((137373.3 45.…
10 Utrecht 3515     GROENLINKS    1240           4257                        29     1091       1.00       2908       3014     473581 MULTIPOLYGON (((136393.6 45.…

That produces the following image:

enter image description here

What's the easiest way to turn each polygon into a dot where I can assign a value to? Help is much appreciated.

Tdebeus
  • 1,519
  • 5
  • 21
  • 43
  • What is the result of `class(utrecht_sf)` and could you show us what you tried and which error message did you get? – nebi Mar 29 '18 at 09:52
  • `utrecht_sf` is a tibble and the answer below from @SymbolixAU worked like a charm! Only I'm having difficulties with attaching two aestatics to the dots. – Tdebeus Mar 29 '18 at 09:55

1 Answers1

3

You can find the centre of each polygon with sf::st_centroid(). This will return POINTS, which you can plot as you wish.

Here's a reproducible example

library(sf)
# devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

nc <- st_read(system.file("shape/nc.shp", package="sf"))

nc_centers <- sf::st_centroid(nc)

ggplot(data = nc_centers) +
  geom_sf(aes(fill = AREA))
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139