Search code examples
rspatialr-sf

plot arrows in SF polygon


What is correct way to plot a curved arrow between centroid of polygons?

I tried the curvedarrow function from diagram package, but it's plotting arrows on some other places, probably due to different cordinates system.

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
plot(st_geometry(nc))
nc$centroid<- st_centroid(nc$geometry)
plot(nc$centroid, add=T, pch=3, col="red")

enter image description here


Solution

  • Following on from your example. To simplify, just use the first four polygons:

    > plot(st_geometry(nc)[1:4])
    

    Get the four polygon centroids. Ignore the warning:

    > xy = st_coordinates(st_centroid(nc)[1:4,])
    Warning messages:
    1: In st_centroid.sf(nc) :
      st_centroid assumes attributes are constant over geometries of x
    2: In st_centroid.sfc(st_geometry(x), of_largest_polygon = of_largest_polygon) :
      st_centroid does not give correct centroids for longitude/latitude data
    

    And draw some curved arrows between centroids:

    > curvedarrow(from=xy[2,],to=xy[1,],lcol="red", curve=.2)
    > curvedarrow(from=xy[4,],to=xy[1,],lcol="red", curve=.2)
    

    that one's curved off the plot, so flatten it a bit:

    > curvedarrow(from=xy[4,],to=xy[1,],lcol="red", curve=.12)
    

    enter image description here

    I've added all the centroids to this plot so you can see it is drawing curves from centroid to centroid.