2

I have a plot like

example plot

My goal is to filter out all the data that falls inside the inner circle.

I have tried ways to filter only by the data that draws the circle, problem is that is has X and Y values and they're in all 4 spaces of a cartesian map, so if a point is above may be okay (filtering by values greater than something) but that would filter out values that are still outside the inner circle but below it.

I've also tried to do things using integrals but my math is rusty. Integrals suppose to be able to recognise values under the curve. If the curve draws the inner circle maybe I'm able to filter values out. For me, right know, that's more easy to say than to do, so help is appreciated.

PS: non-native english speaker here, so pardon me.


The circles are created by:

radii=c(sqrt(1/2),1)
theta <- seq(0, 2 * pi, length = 500)

#Inner Circle:

 xtemp1=radii[1]*cos(theta)
 ytemp1=radii[1]*sin(theta)
 circ1=as.data.frame(cbind(xtemp1,ytemp1))

#Outer Circle:

 xtemp2=radii[2]*cos(theta)
 ytemp2=radii[2]*sin(theta)
 circ2=as.data.frame(cbind(xtemp2,ytemp2))

Here's a small part of the data (named temp):

Comp_1,Comp_2
0.253,-0.29
-0.23,0.222
-0.384,0.432
-0.032,0.805
-0.261,0.265
-0.181,0.344
-0.133,-0.436
-0.358,-0.004
-0.139,-0.314
0.303,0.257
-0.131,0.602

And for the plot:

#Plot
p1=ggplot(data=temp, aes(x=Comp_1, y=Comp_2)) + geom_point() +
  scale_x_continuous(limits=c(-1,1), breaks=seq(-1,1,by=0.1)) + 
  scale_y_continuous(limits=c(-1,1), breaks=seq(-1,1,by=0.1))
#Circle
p1=p1 + geom_polygon(data=circ1, aes(x=xtemp1, y=ytemp1),size=0.5,inherit.aes = F, colour="black", fill=NA, alpha=0.5) +
  geom_polygon(data=circ2, aes(x=xtemp2, y=ytemp2),size=0.5,inherit.aes = F, colour="black", fill=NA, alpha=0.5)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Cris
  • 787
  • 1
  • 5
  • 19

2 Answers2

3

This sounds like a Point in Polygon problem (https://en.wikipedia.org/wiki/Point_in_polygon) . The point.in.polygon() function in the sp library might be usefull.

point.in.polygon(Comp_1,Comp_2,xtemp1,ytemp1)
Koot6133
  • 1,428
  • 15
  • 26
  • Because of the nature of my data this answer was actually more helpful than the one that @xraynaud gave me. Thank you very much both of you. – Cris Sep 29 '17 at 18:53
2

The equation of a circle is x^2 + y^2 = R^2 so any point in your dataset for which x^2 + y^2 < R^2 falls inside a circle having a radius of R.

To remove the points that falls into your inner circle, you just need to

filtered_out = temp[apply(temp^2,1,sum) > radii[1]^2,]
xraynaud
  • 2,028
  • 19
  • 29