Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Last active April 26, 2022 15:09
Show Gist options
  • Save CnrLwlss/c3ee2ddf7a06e0f843d6760a5401578f to your computer and use it in GitHub Desktop.
Save CnrLwlss/c3ee2ddf7a06e0f843d6760a5401578f to your computer and use it in GitHub Desktop.
Interactive selection of points in a scatterplot.
library(sp)
xvals = rnorm(1000)
yvals = rnorm(1000)
alpha = 0.1
# Note you need to terminate by right-clicking and selecting "stop"
plot(xvals,yvals,xlab="x",ylab="y",pch=16,col=rgb(1,0,0,alpha))
res = locator(n=4000, type="o")
selected = point.in.polygon(xvals,yvals,res$x,res$y)==1
plot(NULL,xlim=range(xvals),ylim=range(yvals),xlab="x",ylab="y")
points(xvals[selected],yvals[selected],pch=16,col=rgb(0,0,1,alpha))
points(xvals[!selected],yvals[!selected],pch=16,col=rgb(1,0,0,alpha))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment