# Re: [R] plot the number of replicates at the same point

From: Jim Lemon <bitwrit_at_ozemail.com.au>
Date: Fri 15 Jul 2005 - 18:49:15 EST

Hi all,

This is nowhere near as elegant as Deepayan's solution, but I read the spec as plotting symbols except where there were overlays. You are welcome to improve the following...

Jim

count.overplot<-function(x,y,tol=NULL,...) {  if(missing(x))
stop("Usage: count.overplot(x,y,tol=NULL,...)")  dimx<-dim(x)
# if x is a data frame or matrix with at least two columns, split it  if(missing(y) && !is.null(dimx)) {
y<-x[,2]
x<-x[,1]
}

xlen<-length(x)
if(xlen != length(y)) stop("x and y must be the same length.")  if(is.null(tol)) tol<-c((max(x)-min(x))/1000,(max(y)-min(y))/1000)  flags<-1:xlen
xsep<-ysep<-xdup<-ydup<-xydup<-rep(0,xlen)  nsep<-ndup<-0
for(i in 1:xlen) {
if(!is.na(flags[i])) {
dups<-abs(x - x[i]) <= tol & abs(y - y[i]) <= tol    ndups<-sum(dups)
if(ndups > 1) {
ndup<-ndup+1
xydup<-ndups
xdup[ndup]<-x[i]
ydup[ndup]<-y[i]
}
else {
nsep<-nsep+1
xsep[nsep]<-x[i]
ysep[nsep]<-y[i]
}
}
flags[dups]<-NA
}

plot(xsep[1:nsep],ysep[1:nsep],...)
text(xdup[1:ndup],ydup[1:ndup],xydup[1:ndup]) }

R-help@stat.math.ethz.ch mailing list