Identify the rows of a data.frame lying within a closed lasso polygon, analogous to brushedPoints
.
Details
This function uses in.out
from the mgcv package to identify points within a polygon.
This involves a boundary crossing algorithm that may not be robust in the presence of complex polygons with intersecting edges.
Examples
lasso <- list(coord=rbind(c(0, 0), c(0.5, 0), c(0, 0.5), c(0, 0)),
closed=TRUE, mapping=list(x="X", y="Y"))
values <- data.frame(X=runif(100), Y=runif(100),
row.names=sprintf("VALUE_%i", seq_len(100)))
lassoPoints(values, lasso)
#> X Y
#> VALUE_6 0.349967340 0.043971138
#> VALUE_8 0.084754502 0.003632551
#> VALUE_19 0.014659769 0.341874812
#> VALUE_22 0.119741780 0.009373354
#> VALUE_23 0.049255810 0.233537052
#> VALUE_27 0.119054324 0.014021893
#> VALUE_36 0.005012375 0.324234673
#> VALUE_41 0.317547232 0.033578795
#> VALUE_55 0.420651288 0.011788648
#> VALUE_78 0.048321765 0.451294619
#> VALUE_80 0.188467675 0.244812947
#> VALUE_81 0.025653972 0.422450280
#> VALUE_82 0.086210669 0.122983353
#> VALUE_87 0.174070850 0.020299537
#> VALUE_95 0.063634377 0.223738024
# With faceting information:
lasso <- list(coord=rbind(c(0, 0), c(0.5, 0), c(0, 0.5), c(0, 0)),
panelvar1="A", panelvar2="B", closed=TRUE,
mapping=list(x="X", y="Y",
panelvar1="FacetRow", panelvar2="FacetColumn"))
values <- data.frame(X=runif(100), Y=runif(100),
FacetRow=sample(LETTERS[1:2], 100, replace=TRUE),
FacetColumn=sample(LETTERS[1:4], 100, replace=TRUE),
row.names=sprintf("VALUE_%i", seq_len(100)))
lassoPoints(values, lasso)
#> X Y FacetRow FacetColumn
#> VALUE_51 0.1303181 0.2821039 A B