rggplot2spatialr-sf

How to avoid rough jagged buffer edges


I'm working on a project where I need to plot a buffer around a centroid in R. I'm also using this centroid buffer to do an intersection with another geometry.

The problem I'm facing is that the buffer border is not a very well defined circle and doesn't look good when I plot both the circle and the intersection.

My code looks somewhat like this:

library(sf)
library(ggplot2)

circle <- st_buffer(centroid, 100000)

intersection <- st_intersection(geometry,circle)

ggplot() + 
  geom_sf(data = circle, fill = "yellow")

The border of buffer is "low quality": The border of buffer is "low quality"


Solution

  • Not so bad! That said, if you really want to smooth the circle/buffer border, you can use the package smoothr.

    Here is one possibility :

    library(smoothr)
    
    # Smooth circle border
    circle_smooth <- smooth(densify(circle, max_distance = 10), method = "ksmooth")
    
    # Plot
    ggplot2::ggplot()+
      ggplot2::geom_sf(data= circle_smooth, fill = "yellow")