rplotrix

R Need to put a break in y axis


I need to put a break in y-axis between 25,000 and 200,000. Here is the code and I can't figure out why it doesn't give me the y-axis I need. I need the first portion to go from 0 to 25,000 in steps of 5,000 and the second part to go from 200,000 to 2 million in steps of 500K.

x <- c(1000000, 5000000, 10000000, 15000000, 20000000, 25000000, 
       30000000, 35000000, 40000000, 45000000, 50000000, 55000000,
       60000000, 65000000, 70000000, 75000000, 80000000, 85000000,
       90000000, 95000000, 100000000)

y <- c(3305.8, 4175.4, 4175.4, 4201.7, 4201.7, 23529.4, 23529.4, 23529.4, 23529.4,
       23529.4, 23529.4, 23529.4, 23529.4, 23529.4, 23529.4, 23529.4, 23529.4,
       285714.3, 2000000.0, 2000000.0, 2000000)

library(plotrix)
gap.plot(x, y, gap = c(25000, 200000), gap.axis = "y", ylim = c(0, 2000000),
             ytics = c(seq(0, 25000, by = 5000), seq(200000, 2000000, by = 500000)))
        axis.break(axis = 2, breakpos = 25000, style = "slash")

Solution

  • You can create a bit of a work-around for this issue. I'm assuming you want the plot to be more readable. You can reduce the magnitude of the very large values to make them more comparable to the smaller values while keeping the original axis labels. That is,

    y2 <- ifelse(y>25000, y/100 + 25000, y)
    gap.plot(x,y2,gap=c(25000,25000), gap.axis="y",ylim=c(0,2000000/100 + 25000),
             ytics=c(seq(0,25000,by=5000),seq(27000,45000,by=5000)),
             yticlab=c(seq(0,25000,by=5000),seq(200000,2000000,by=500000)))
    axis.break(axis=2,breakpos=25000,style="slash")