Can borders be drawn around groups of points / clusters using ggplot2?

Here's a toy example with mtcars. Suppose everything to the left of 200 on the x-axis ('disp' here) is a cluster, and everything to the right of 200 is a different cluster.

ggplot(mtcars, aes(x=disp, y=qsec)) + 
  geom_point() +

Is there some way to draw borders ("gate") around the two groups in a non-uniform way? I've seen ways to draw circles or ellipses, but these are always uniform. I think some solution may lie within the ggforce package, but I'm not sure how to implement that.

Looking for something like this:

ggplot(mtcars, aes(x=disp, y=qsec)) + 
  geom_point() +
  geom_vline(xintercept=200) +
  ggforce::geom_mark_hull(aes(group = disp > 200))
  # tweak the concavity, expand, and radius parameters to taste

ps - I had to install the concaveman package when I got this message:

Warning message:
The concaveman package is required for geom_mark_hull 
> install.packages("concaveman")

Thanks, this definitely solved my problem!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.