[R] axis break in R

Marc Girondot marc_grt at yahoo.fr
Sun Feb 28 16:34:52 CET 2016


Hi Eike,

I didn't know how to do, so it was a nice exercise. Here is a solution 
using plot(). I have made a function plot.break(); the name of 
parameters of the function are self-explained (I think), but tell me if 
you need more explanation.
You can add any parameters of the plot() function.

I have made this function very fast and it is not fully tested. If you 
see a problem, do not hesitate to tell me.

Sincerely

Marc

plot.break <- function(x, y, at.cut, at.continue, add.cut,
                        number.ticks.bottom, number.ticks.up,
                        min.y, max.y, ...) {

   p.plot <- list(...)
   p.plot <- modifyList(list(x = x, y=ifelse(y>at.cut, 
y-at.continue+add.cut, y),
                             bty="n", type="p", las=1,
                             ylim=c(min.y, max.y-at.continue+add.cut), 
yaxt="n", xlab="x", ylab="y"),
                        p.plot)
   do.call(plot, p.plot)
   y.axis <- c(seq(from=0, to=at.cut, length=number.ticks.bottom),
               seq(from=at.cut+add.cut, to=ScalePreviousPlot()$ylim[2], 
length=number.ticks.up))
   axis(2, at=y.axis,
        labels=ifelse(y.axis>at.cut, y.axis+at.continue-add.cut, y.axis),
        las=1)
   par(xpd=TRUE)

   x.left <- 
ScalePreviousPlot()$xlim[1]-0.05*ScalePreviousPlot()$xlim["range"]
   x.right <- 
ScalePreviousPlot()$xlim[1]+0.00*ScalePreviousPlot()$xlim["range"]

   polygon(x=c(x.left, x.right, x.right, x.left, x.left),
           y=c(at.cut+0.5, at.cut+0.5, at.cut+add.cut-0.5, 
at.cut+add.cut-0.5, at.cut+0.5),
           col="white", border=NA)
}

x <- 1:10
y <- c(rnorm(5, 10, 5), rnorm(5, 100, 5))

library(HelpersMG)
at.cut <- 20
at.continue <- 60
add.cut <- 5
number.ticks.bottom <- 3
number.ticks.up <- 5
min.y <- 0
max.y <- 120

plot.break(x = x, y=y, at.cut= 20,
            at.continue= 60,
            add.cut= 5,
            number.ticks.bottom= 3,
            number.ticks.up= 5,
            min.y= 0,
            max.y= 120, xlab="Value x")


Le 23/02/2016 11:00, Eike Marie Thaysen a écrit :
> Hello,
> I want to break the y-axis on one of my plots. I read this is possible- but
> apparently only with the plotrix package that requieres R version 3.6-1? I
> cannot seem to find that version on the R homepage.. it is a version you
> have to pay for? Are there any other possebilities to do it (I am currently
> running (for R version 3.1.2) )
> Thank you,
> Eike
>
>



More information about the R-help mailing list