Show complete plot line in gap.plot

1.4k Views Asked by At

I am trying to create a gap.plot using the plotrix package in R. I can create the broken axes well enough, but I'm trying to show a broken line within the plot at the same location and have drawn a blank.

Here's what I have so far;

library(plotrix)
library(Hmisc)

# some sample data    
days<-c(30,44,62,70,102,435)
result<-c(-0.4290, -0.4430, -0.4630, -0.4160, -0.5280, -0.4125)
sem<-c(0.027, 0.02, 0.023, 0.027, 0.023, 0.0157)

## make a second set of day values so the error bars can be added to the RHS plot
adj_days<-days-300
dat<-data.frame(days,result,sem,adj_days)


par(mfrow=c(1,1))
par(mar=c(2.5,2.5,2.5,2.5))    
par(tck=-0.02)
par(cex.axis=0.6)
par(mgp=c(0,0.3,0))

## plot the data

gap.plot(days,result,gap=c(120,420),gap.axis = "x",
         pch=21,type="o",col="black",xtics=c(435,435),xticlab=c(435),
         yticlab=NA,ytics=c(-1,1),ylim=c(-0.6,-0.2),xlim=c(20,450),xlab=NA,ylab=NA)

## add error bars

##LHS    

with (data=dat,expr=errbar(days, result, result+sem,result-sem, 
                                  add=T, pch=NA,cex=0.8,cap=0.01))

##RHS

with (data=dat,expr=errbar(adj_days, result, result+sem,result-sem, 
                           add=T, pch=NA,cex=0.8,cap=0.01))





 ## add axis lables    

    axis(side=1,at=c(30,45,62,70,102,435),labels=T,
                 cex.axis=0.6,cex.lab=0.6,mgp=c(0,0.3,0),tck=-0.02)

    axis(side=2,line=0,tck=-0.015,at=seq(-0.6,-0.2,0.1),las=1,cex.axis=0.6,
                 cex.lab=0.6,mgp=c(0,0.4,0))

   ## add axis breaks
   abline(v=seq(120,122.8,.001), col="white")
            axis.break(3,121,style="slash")        
            axis.break(1,121,style="slash")

And here's what the plot looks like

Example gap.plot

How do I continue the line between the points at day 102 and day 435??

Any help greatly appreciated.

Thanks in advance

1

There are 1 best solutions below

0
On

So I figured it out, and in the hopes of assisting someone else who may have the same problem, here is the solution

Simply add the following lines of code after the axis commands and before the axis breaks

X<-c(102,135)
Y<-c(-0.528,-0.4125)
par(new=T)
plot(X,Y,type="l",xlim=c(20,150),ylim=c(-0.6,-0.2),xaxt="n",yaxt="n")

Now the plot looks like this

Solved