# # A wrapper around the function 'plot()' that jitters variables with # 8 or less values, and provides more space around the pointclouds. # [The implementation is a nasty hack: it adds to more extreme points to the data...] # # The number 8 can be changed. # The jitter amount is .4 of the smallest spacing between # different values. # Again, the number .4 can be changed. # # One of the great annoyances in R's scatterplots is that the extreme # points are too close to the borders (top and right) and axes (bottom and left). # This function provides 10% of the range on both sides as extra space. # The number 10% can be changed with the 'extra' argument. # # [Also, the plotting character default is changed to 16, and # plotting character size to .8.] # plot.plus <<- function(dat, max.ncat=8, jitter=.1, extra=.1, lims=NULL, cex=0.8, pch=16, col="black", ...) { for(j in 1:ncol(dat)) if(!(class(dat[,j]) %in% c("numeric","integer"))) dat[,j] <- c(factor(dat[,j])) # Jitter: for(i in 1:ncol(dat)) { x <- dat[,i]; xvals <- unique(x) if(length(xvals) < max.ncat) { space <- min(diff(sort(xvals))) dat[,i] <- x + runif(length(x), -space*jitter, space*jitter) } } # Add extra space around the points by adding two points that are not drawn: if(is.null(lims)) { mins <- apply(dat, 2, min); maxs <- apply(dat, 2, max); ranges <- maxs-mins dat <- rbind(mins-extra*ranges, maxs+extra*ranges, dat) } else { dat <- rbind(lims[1], lims[2], dat) } # Handle addition of two points in col, cex, pch: col <- c(rep(par()$bg,2), rep(col,len=nrow(dat)-2)) pch <- c(rep(1,2), rep(pch,len=nrow(dat)-2)) # Finally: plot(dat, cex=cex, pch=pch, col=col, ...) }