当前位置:天才代写 > tutorial > 其他教程 > R画图基本(三)坐标间断(axis breaks)

R画图基本(三)坐标间断(axis breaks)

2017-12-04 08:00 星期一 所属: 其他教程 浏览:794

R傍边的坐标间断一般都利用plotrix库中的axis.break(), gap.plot(), gap.barplot(), gap.boxplot()等几个函数来实现,例:

R绘图根基(三)坐标中断(axis breaks)

gap plot

> library(plotrix)
> opar<-par(mfrow=c(3,2))
> plot(sample(5:7,20,replace=T),main="Axis break test",ylim=c(2,8))
> axis.break(axis=2,breakpos=2.5,style="gap")
> axis.break(axis=2,breakpos=3.5,style="slash")
> axis.break(axis=2,breakpos=4.5,style="zigzag")
> twogrp<-c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22)
> gap.plot(twogrp,gap=c(8,16,25,35),
+  xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
+  main="Test two gap plot with the lot",xtics=seq(0,30,by=5),
+  ytics=c(4,6,18,20,22,38,40,42),
+  lty=c(rep(1,10),rep(2,10)),
+  pch=c(rep(2,10),rep(3,10)),
+  col=c(rep(2,10),rep(3,10)),
+  type="b")
> gap.plot(21:30,rnorm(10)+40,gap=c(8,16,25,35),add=TRUE,
+   lty=rep(3,10),col=rep(4,10),type="l")
> gap.barplot(twogrp,gap=c(8,16),xlab="Index",ytics=c(3,6,17,20),
+   ylab="Group values",main="Barplot with gap")
> gap.barplot(twogrp,gap=c(8,16),xlab="Index",ytics=c(3,6,17,20),
+   ylab="Group values",horiz=TRUE,main="Horizontal barplot with gap")
> 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="Show outliers separately")
> 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)

利用上面的步伐可以绘制出双反斜线间断,并可以视实际环境加油断点起止位置。

R绘图根基(三)坐标中断(axis breaks)

改造的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)

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式