Mastering R plot -3笔记

来源:互联网 发布:推广淘宝店 编辑:程序博客网 时间:2024/06/05 00:26

图形都会有内外边界,默认情况我们看不到外边界

par()$oma # 外边界[1] 0 0 0 0par()$usr # 作图区域的范围限制[1] 0.568 1.432 0.568 1.432
op <- par(no.readonly = TRUE)par(oma = c(1,1,2,2))plot(1,1,type = "n",xlab = "",ylab = "",xaxt = "n",yaxt = "n")for(side in 1:4){  inner <- round(par()$mar[side],0)-1  for(line in 0:inner)  {    mtext(text = paste0("Inner Line",line),side = side,          line = line)  }  outer <- round(par()$oma[side],0)-1  for(line in 0:inner)  {    mtext(text = paste0("Outer line",line),side = side,          line = line,outer = TRUE)  }}par(op)

dev.off()可以关掉图形设备
mar 设置图形边界空白宽度,默认为c(5,4, 4, 2) + 0.1
oma 设置外边界宽度,类似mar,默认为c(0,0,0,0)
mtext()在外边界(图形区域外)写入文本,需要设置outer=true

当坐标轴标签太长,而又不想缩短时,有两种解决方法
1.增大图形边界(相应作图区域会缩小)
2.允许拓展到图形区域外部

par(cex.lab=1.7)plot(1,1,ylab="A very very long axis title\nthat need special care",xlab="",type="n")#one option would be to increase inner margin sizepar(mar=c(5,7,4,2))plot(1,1,ylab="A very very long axis title\nthat need special care",xlab="",type="n")#sometime this is not desirable so one may plot the axis text outside of the plotting areapar(op)par(oma=c(0,4,0,0))plot(1,1,ylab="",xlab="",type="n")mtext(text="A very very long axis title\nthat need special care",      side=2,line=0,outer=TRUE,cex=1.7)

在外边界可以把坐标轴标签设置成大号的,不用考虑是否超出作图区域

当多图共有一个横纵坐标轴标签时:向图形外边界添加文本
mtext() 边界文本函数,设置参数outer=TRUE

多图共有一个图例:legend()函数,xpd参数
xpd=FALSE(默认) 将图形元素(文本图例、点、线)限制在作图区域内;
xpd=TRUE 将图形元素限制在图形区域,即作图区域+inner margins;
xpd=NA 则图形元素可以在整个图形设备区域内

par(op)par(oma=c(3,3,0,0),mar=c(3,3,2,2),mfrow=c(2,2))plot(1,1,ylab="",xlab="",type="n")plot(1,1,ylab="",xlab="",type="n")plot(1,1,ylab="",xlab="",type="n")plot(1,1,ylab="",xlab="",type="n")mtext(text="A common x-axis label",side=1,line=0,outer=TRUE)mtext(text="A common y-axis label",side=2,line=0,outer=TRUE)legend(x = 1.4,y = 1.2,legend=LETTERS[1:5],col = unique(cols),       pch = 16,bty = "n",xpd = NA)
0 0
原创粉丝点击