How to find outer points in 2 vectors?

Viewed 54

How can I find out which are the outer points in a 2d area? See the following plot:

Plot

I need to know what the outer points are, i.e. points that fulfull the following: if we connect the points with a line all data lays inside. The red line correctly connects those outer points. On the other hand, the green line is wrong because it excludes some points and the blue line is wrong because it unnecessarily goes to a point that can be included by connecting other outer points (as the red line does).

Data and code:

set.seed(3)
df <- data.frame(matrix(rnorm(100), ncol= 2))
library(ggplot2)
ggplot() +
  geom_point(data= df, mapping= aes(X1, X2))  

My expected output is a dataframe with a third column containing a TRUE if the point in the row is an outer point. I look for a base R solution.

1 Answers

To avoid "answers" in comments:

Use

df$outer <- FALSE
df$outer[chull(df$X1, df$X2)] <- TRUE
ggplot() +
  geom_point(data= df, mapping= aes(X1, X2)) +
  geom_point(data= df[df$outer, ], mapping= aes(X1, X2), col= "red")

See the results:

Plot

Thanks to @missuse

Related