Search code examples
rggplot2spatialr-sf

Is there a way to have better buffer borders in R?


I'm hoping maybe someone can help me with the following situation. I'm working on a project were I need to plot a circle (radius) around a centroid in R. I'm also using this centroid to do an intersection with other geometry. The problem I'm facing is that the buffer border is not a very well defined circle which doesn't look good when I plot both the circle and the intersection.

My code looks somewhat like this:

using sf and ggplot2

Radius of 100km circle = st_buffer(centroid,100000) and for the intersection

intersection = st_intersection(geometry,circle)

for the plot

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

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")