Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the points inside of the ellipse in ggplot2?

Tags:

plot

r

ggplot2

I'm trying to identify the densest region in the plot. And I do this using stat_ellipse() in ggplot2. But I can not get the information (sum total, order number of each point and so on) of the points inside of the ellipse.

Seldom see the discussion about this problem. Is this possible?

For example:

ggplot(faithful, aes(waiting, eruptions))+
    geom_point()+
    stat_ellipse()

enter image description here

like image 447
yingjc Avatar asked Oct 19 '25 03:10

yingjc


1 Answers

Here is Roman's suggestion implemented. The help for stat_ellipse says it uses a modified version of car::ellipse, so therefore I chose to extract the ellipse points from the ggplot object. That way it should always be correct (also if you change options in stat_ellipse).

# Load packages
library(ggplot2)
library(sp)

# Build the plot first
p <- ggplot(faithful, aes(waiting, eruptions)) +
  geom_point() +
  stat_ellipse()

# Extract components
build <- ggplot_build(p)$data
points <- build[[1]]
ell <- build[[2]]

# Find which points are inside the ellipse, and add this to the data
dat <- data.frame(
  points[1:2], 
  in.ell = as.logical(point.in.polygon(points$x, points$y, ell$x, ell$y))
)

# Plot the result
ggplot(dat, aes(x, y)) +
  geom_point(aes(col = in.ell)) +
  stat_ellipse()

enter image description here

like image 54
Axeman Avatar answered Oct 21 '25 16:10

Axeman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!