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

8 Comments
davidz
Thank you! Looks helpful, however I get the error: could not find function "geom_encircle". I have ggplot2 and ggalt installed. Weird.
GGamba
also updated and loaded? On my system:
packageVersion('ggplot2'): ‘2.2.1’ packageVersion('ggalt'): ‘0.4.0’davidz
remove.packages("ggalt") install.packages("ggalt") update.packages("ggalt") packageVersion("ggalt") gives [1] ‘0.1.1’GGamba
'ggalt' on CRAN is at version 0.4.0
GGamba
It requires
ggplot2 version 2.2.1. Is ggplot updated too? |
sos::findFn("convexhull")I'd try eitherspatstatordismo. It appears from the illustration ofggalt::geom_encirclebelow, that it might also be a convex result.