title: r sf package centroid within polygon
body: <p>I need to add labels to polygons and I normally use the centroid, however the centroid does not fall inside the polygon.  I found this question <a href="https://stackoverflow.com/questions/44327994/calculate-centroid-within-inside-a-spatialpolygon">Calculate Centroid WITHIN / INSIDE a SpatialPolygon</a> but I'm using the sf package.</p>
  
  <p>Below is a toy data</p>
  
  <pre><code>rm(list = ls(all = TRUE)) #start with empty workspace
  
  library(sf)
  library(tidyverse)
  library(ggrepel)
  
  pol &lt;- st_polygon(list(rbind(c(144, 655),c(115, 666)
                           ,c(97, 660),c(86, 640)
                           ,c(83, 610),c(97, 583)
                           ,c(154, 578),c(140, 560)
                           ,c(72, 566),c(59, 600)
                           ,c(65, 634),c(86, 678)
                           ,c(145, 678),c(144, 655)))) %&gt;%
    st_sfc()
  
  a = data.frame(NAME = "A")
  st_geometry(a) = pol
  
  a &lt;- a  %&gt;% 
    mutate(lon = map_dbl(geometry, ~st_centroid(.x)[[1]]),
       lat = map_dbl(geometry, ~st_centroid(.x)[[2]]))
  
  ggplot() +
    geom_sf(data = a, fill = "orange") +
    geom_label_repel(data = a, aes(x = lon, y = lat, label = NAME)) 
  </code></pre>
  
  <p>which results in the following</p>
  
  <p><a href="https://i.stack.imgur.com/xL8hg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xL8hg.png" alt="enter image description here"></a></p>