# 【1.7】坐标中断(axis breaks)-- plotrix

R当中的坐标中断一般都使用plotrix库中的axis.break(), gap.plot(), gap.barplot(), gap.boxplot()等几个函数来实现，例：

## axis.break

> install.packages("plotrix")
> library(plotrix)
> opar<-par(mfrow=c(3,2))  #将画板分区
> plot(sample(5:7,20,replace=T),main="A ",ylim=c(2,8))
> axis.break(axis=2,breakpos=2.5,style="gap")
> plot(sample(5:7,20,replace=T),main="B",ylim=c(2,8))
> axis.break(axis=2,breakpos=3.5,style="slash")
#> plot(sample(5:7,20,replace=T),main="各个 ",ylim=c(2,8))
#> axis.break(axis=2,breakpos=4.5,style="zigzag")


help(axis.break)

axis.break(axis=1,breakpos=NULL,pos=NA,bgcol="white",breakcol="black",
style="slash",brw=0.02)

axis：      which axis to break
breakpos         ：where to place the break in user units
pos：      position of the axis (see axis)
bgcol：   the color of the plot background
breakcol：      the color of the "break" marker
style         ：  Either gap, slash or zigzag
brw：      break width relative to plot width


## gap.plot

twogrp<-c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22);
gpcol<-c(2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5);
gap.plot(twogrp,gap=c(8,16),xlab="Index",ylab="Group values", main="E ",col=gpcol);
gap.plot(twogrp,rnorm(20),gap=c(8,16),gap.axis="x",xlab="Xvalues",xtics=c(4,7,17,20),ylab="Y values",main="D");


help(gap.plot)

gap.plot(x,y,gap,gap.axis="y",bgcol="white",breakcol="black",brw=0.02,xlim=range(x),ylim=range(y),xticlab,xtics=NA,yticlab,ytics=NA,lty=rep(1,length(x)),col=rep(par("col"),length(x)),pch=rep(1,length(x)),add=FALSE,stax=FALSE,...)

x,y:    data values
gap:   the range(s) of values to be left out  省略的轴
gap.axis:   whether the gaps are to be on the x or y axis   在哪个轴上省略
bgcol:       the color of the plot background
breakcol:  the color of the "break" marker
brw:         break width relative to plot width
xlim,ylim:         the plot limits.
xticlab:    labels for the x axis ticks
xtics          :  position of the x axis ticks  #x轴显示的表号
yticlab:    labels for the y axis ticks
ytics          :  position of the y axis ticks
lty:     line type(s) to use if there are lines
col:  color(s) in which to plot the values
pch:   symbols to use in plotting.
stax:           whether to call staxlab for staggered axis labels.


## gap.barplot

#> gap.barplot(twogrp,gap=c(8,16),xlab="Index",ytics=c(3,6,17,20),ylab="Group values",main="F")
> gap.barplot(twogrp,gap=c(8,16),xlab="Index",ytics=c(3,6,17,20),ylab="Group values",horiz=TRUE,main="E")
> twovec<-list(vec1=c(rnorm(30),-6),vec2=c(sample(1:10,40,TRUE),20))
>  gap.boxplot(twovec,gap=list(top=c(12,18),bottom=c(-5,-3)),main="F")
> gap.boxplot(twovec,gap=list(top=c(12,18),bottom=c(-5,-3)),range=0,main="Include outliers in whiskers")
> par(opar)  #关闭当前，返回上一次的画板


> axis.break(2,from,breakcol="snow",style="gap")
> axis.break(2,from*(1+0.02),breakcol="black",style="slash")
> axis.break(4,from*(1+0.02),breakcol="black",style="slash")
> axis(2,at=from)


## 改进的gap.plot

> library(plotrix)
> x<-c(1:5,6.9,7)
> y<-2^x
> from<-33
> to<-110
> opar<-par(mfrow=c(2,2))
>plot(x,y,type="b",main="normal plot")

> gap.plot(x,y,gap=c(from,to),type="b",main="gap plot")
> axis.break(2,from,breakcol="snow",style="gap")
> axis.break(2,from*(1+0.02),breakcol="black",style="slash")
> axis.break(4,from*(1+0.02),breakcol="black",style="slash")
> axis(2,at=from)

> gap.barplot(y,gap=c(from,to),col=as.numeric(x),main="barplot with gap")
> axis.break(2,from,breakcol="snow",style="gap")
> axis.break(2,from*(1+0.02),breakcol="black",style="slash")
> axis.break(4,from*(1+0.02),breakcol="black",style="slash")
> axis(2,at=from)

> gap.barplot(y,gap=c(from,to),col=as.numeric(x),horiz=T,main="Horizontal barplot with gap")
> axis.break(1,from,breakcol="snow",style="gap")
> axis.break(1,from*(1+0.02),breakcol="black",style="slash")
> axis.break(3,from*(1+0.02),breakcol="black",style="slash")
> axis(1,at=from)
> par(opar)


## 参考资料：

http://www.dataguru.cn/article-4827-1.html