rggplot2linepolygonpoints

Looking for a way to determine how many points are above a certain (nonlinear) line in R


I have a big data frame (df) which contains x/y coordinates and a nonlinear regression line that is somewhere in the middle, see plot below. Many points overlap, that's why I have an extra column $Freq.

Points (a lot of them are overlapping) and the line.

I am looking for a way to determine how many points (many on top of eachother) are above this line. See the df structure below.

head(df)
    x   y  Freq
    0   0  396
    1   1  222
    1   0  513
    2   0  315
    2   1  279
    2   2   36
...

I am aware of the polygon methods here on StackOverflow but I can't seem to get those to work, maybe in part due to the fact that my line is a series of coordinates rather than a formula:

head(line)
x    y
0 0.0000
1 0.4220
2 0.8350
3 1.2545
4 1.6615
5 2.0450

In the end it would be great if I can have three numbers: one that describes the count of the points above the line, the count of the points below the line, and possibly those that are to the right of this particular line.

Thanks!


Solution

  • A lot of unknowns here, nevertheless

    df=merge(df,line,by="x",suffixes=c("_df","_line"))
    sum(df$Freq[df$y_df>df$y_line])
    
    [1] 537