1

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

davidz
  • 13
  • 5
  • There is a convexhull function is some package or another. But as it's name implies it will be convex. After `sos::findFn("convexhull")` I'd try either `spatstat` or `dismo`. It appears from the illustration of `ggalt::geom_encircle` below, that it might also be a convex result. – IRTFM Jun 20 '17 at 21:53

1 Answers1

3

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

GGamba
  • 13,140
  • 3
  • 38
  • 47
  • Thank you! Looks helpful, however I get the error: could not find function "geom_encircle". I have ggplot2 and ggalt installed. Weird. – davidz Jun 20 '17 at 21:00
  • also updated and loaded? On my system: `packageVersion('ggplot2'): ‘2.2.1’` `packageVersion('ggalt'): ‘0.4.0’` – GGamba Jun 20 '17 at 21:03
  • `remove.packages("ggalt") install.packages("ggalt") update.packages("ggalt") packageVersion("ggalt")` gives [1] ‘0.1.1’ – davidz Jun 20 '17 at 21:18
  • 'ggalt' on [CRAN is at version 0.4.0](https://cran.r-project.org/web/packages/ggalt/) – GGamba Jun 20 '17 at 21:20
  • It requires `ggplot2` version 2.2.1. Is ggplot updated too? – GGamba Jun 20 '17 at 21:21
  • `remove.packages('ggplot2') install.packages('ggplot2') packageVersion('ggplot2')` gives me ‘2.1.0’. I noticed that line `update.packages('ggplot2')` does not do anything. I think that's where the problem is. Thanks for guiding me through this. – davidz Jun 20 '17 at 21:26
  • I had to install ggplot2 source package downloaded from CRAN. `install.packages('ggplot2')` would only install older, binary version. Hope it helps to all confused people like me. Thank you again. – davidz Jun 20 '17 at 21:34
  • If you decide this answer is helpful, [pls consider accepting it](https://stackoverflow.com/help/someone-answers) – GGamba Jun 20 '17 at 21:35