Mastering R plot -3笔记

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

par()$oma # 外边界
[1] 0 0 0 0

par()$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 size
par(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 area
par(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值