【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.
add:            whether to add values to an existing plot.
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)  #关闭当前,返回上一次的画板

从图像效果上来看,这样的坐标中断只能说实现了坐标中断,但效果上是非常一般的。甚至远不如excel, openoffice当中出图效果好。为此,我们需要对plotrix库中的gap.plot做出修改,以达到满意的效果。

最简单的修改办法就是在使用了gap.plot, gap.barplot, gap.boxplot之后重新使用axis.break来修改中断类型,使得看上去美一点。

> 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

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn