davidz
davidz

Reputation: 13

ggplot: How to draw contour line for 2d scatter plot to outline the data points

I need to draw a line around all of my data points that plot within x-y space. I do not need 2d density distribution. See the picture attached (the field was just drawn manually). Thank you. scatter plot with line around data points

Upvotes: 1

Views: 2170

Answers (1)

GGamba
GGamba

Reputation: 13680

This is the perfect use case for ggalt's geom_encircle:

#install.packages('ggalt')
library(ggalt)
#> Loading required package: ggplot2

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  geom_encircle()

You can also encircle by group:

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  geom_encircle()

Upvotes: 4

Related Questions