[R] how can I count data points outside the main plot line?

Giorgio Garziano giorgio.garziano at ericsson.com
Tue Mar 29 12:33:14 CEST 2016


As a "quick solution", I would explore the use of stat_smooth() and then extract fit data from,
as herein shown:

library(ggplot2)
p <- qplot(hp, wt, data=mtcars) + stat_smooth(method="loess")
p
ggplot_build(p)$data[[2]]

           x        y     ymin     ymax        se PANEL group  colour   fill
1   52.00000 1.993594 1.149150 2.838038 0.4111133     1    -1 #3366FF grey60
2   55.58228 2.039986 1.303264 2.776709 0.3586695     1    -1 #3366FF grey60
3   59.16456 2.087067 1.443076 2.731058 0.3135236     1    -1 #3366FF grey60

Reference:

http://stackoverflow.com/questions/9789871/method-to-extract-stat-smooth-line-fit


In place of mtcars, your dataset.

Then, some more work to compare each (x,y) of your dataset with {(x,ymin), (x.ymax)} of
table above and to determine if (x,y) point stays within the smooth lines band or not.

Anyway, I would be interested in hearing about other approaches.

--

Best,

GG

	[[alternative HTML version deleted]]



More information about the R-help mailing list