ggplot: How to draw contour line for 2d scatter pl

2019-08-01 10:55发布

问题:

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

回答1:

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