[R] breaking an axis (skipping part of it)

ben@zoo.ufl.edu ben at zoo.ufl.edu
Mon Mar 5 19:55:54 CET 2001


  These generally have to be done by hand, in part because this kind of
manipulation is going to vary a lot on a case-by-case basis.  However,
here's something to get you started -- a function to draw a break in an
axis, and some sample code (at the bottom) using it.

axis.break <- function(brkpos, which = "y", brkwid = 0.02, brksep = 0.01,
	brkslant = 0.02, oldstyle=FALSE) {
##
##  brkpos: position of the break along the chosen axis
##  which:  which axis to break (bottom ("x") or left ("y")
#    dimensions (given in proportion of chosen axis)
##    brkwid: width of the axis break lines perpendicular to the axis
##    brksep: separation between two axis breaks/height
##    brkslant: (only used for oldstyle) parameter determining angle
##        of axis breaks
##  oldstyle = two parallel lines, new style=zigzag
##
## FIXME: this would be easier to do in inches rather than in user units:
##   wouldn't need to mess around with log/linear scale adjustments
        par.old <- par(no.readonly=TRUE)
	par(xpd = TRUE)  ## useful to set clipping off
	usrax <- par("usr")
	if(which == "x") {
             # break in x axis
		axpos <- usrax[3]
		axwid1 <- usrax[2] - usrax[1]	# x-axis width
		axwid2 <- usrax[4] - usrax[3]	# y-axis width
                otherax <- "y"
                tmpseg <- function(...) segments(...,xpd=TRUE)
	}
	else {
             # break in y axis
		axpos <- usrax[1]
		axwid2 <- usrax[2] - usrax[1]	# x-axis width
		axwid1 <- usrax[4] - usrax[3]	# y-axis width
                tmpseg <- function(w, x, y, z, ...) {
                     segments(x, w, z, y, xpd=TRUE, ...)        # scramble arguments
                }
                otherax <- "x"
	}
	brkwid <- brkwid * axwid2
	brksep <- brksep * axwid1
	brkslant <- brkslant * axwid1
	thislog <- (par(eval(paste(which, "axt", sep = ""))) == "l")
	otherlog <- (par(eval(paste(otherax, "axt", sep = ""))) == "l")
        if (thislog) brkpos <- log10(brkpos)
        if (thislog & otherlog)
	   plotf <- function(w, x, y, z)
	      (tmpseg(10^w, 10^x, 10^y, 10^z))
        else if (thislog & !otherlog)
	   plotf <- function(w, x, y, z)
	      (tmpseg(10^w, x, 10^y, z))
        else if (!thislog & otherlog)
	   plotf <- function(w, x, y, z)
	      (tmpseg(w, 10^x, y, 10^z))
	else # linear scale
	   plotf <- tmpseg
        if (oldstyle) {
           plotf(brkpos + brksep - brkslant, axpos - brkwid, brkpos + brksep +
              brkslant, axpos + brkwid)
           plotf(brkpos - brksep - brkslant, axpos - brkwid, brkpos - brksep +
              brkslant, axpos + brkwid)
	 } else {
           plotf(brkpos + brksep, axpos, brkpos - brksep, axpos, col=par("bg"))
           plotf(brkpos + brksep, axpos, brkpos + brksep/2, axpos - brkwid)
           plotf(brkpos + brksep/2, axpos-brkwid, brkpos - brksep/2, axpos + brkwid)
           plotf(brkpos - brksep/2, axpos+brkwid, brkpos - brksep, axpos)
         }
	invisible(par(par.old))
}


## example

x <- c(1:10)
y <- c(1:9,100)
axbrkstart <- 10
axbrkstop <- 100
axbrkwid <- axbrkstop-axbrkstart
y2 <- y
y2[y2>=axbrkstart] <- y2[y2>=axbrkstart]-axbrkwid
plot(x,y2,axes=FALSE,ylim=c(0,11))
box(bty="l")
axis(side=1)
axis(side=2,at=seq(0,8,by=2))
axis(side=2,at=10,labels="100")
axis.break(9.5)

On Mon, 5 Mar 2001, Moritz Lennert wrote:

> Hello,
>
> Is it possible to skip part of an axis in order to adapt the axis to the
> "normal" values and not to an outlier, but still have the outlier in the
> graph ? I.e. have my axis go from 1 to 100, then break the axis in some
> way, and continue with 500 to 600 ?
>
> Moritz
>
> P.S. Please cc the answer to my address since I'm not in the mailing
> list.
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list