R语言绘图布局实例讲解

这篇文章主要介绍了R语言绘图布局实例讲解,文中实例讲解的很清晰,有感兴趣的同学可以研究下

在R语言中,par 函数可以设置图形边距,其中oma 参数设置outer margin, mar 参数设置margin,

这些边距有什么不同呢,通过box函数可以直观的看到

box 默认在当前图形绘制边框,第一个参数which = "plot", 所以在当前图形上绘制边框

which 的值除了plot 之外,还可以选择 figure, inner, outer

接下来分别用不同的值测试一下,为了区分,为不同的边框设置不同的颜色和类型,代码如下:


attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
box(which = "plot", col = "red", lwd = 2)
box(which = "figure",col = "blue", lwd = 4)

为了区分,plot的边框为 红色, figure 的边框为 蓝色,效果如下:


par(oma=c(1,1,1,1), mar=c(2,2,2,2))
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
box(which = "plot", col = "red",  lwd = 2)
box(which = "figure",col = "blue",  lwd = 4)

效果如下:


par(oma=c(2,2,2,2))
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
box(which = "plot", col = "red",  lwd = 2)
box(which = "figure",col = "blue",  lwd = 4)
box(which = "outer", col = "black", lty = 8)

图中虚线对应的就是outer 边框,在outer 和 figure 边框之间的就是 outer margin , 通过oma 参数的值来设置outer margin 的宽度,和mar 类似,也是从下方开始,沿着逆时针方向进行设置

通过上面的几个例子,我们就可以看到,在R语言中,在一个绘图设备上有3个不同的边框,最外圈为outer 边框,可以看作整张纸的边沿,而中间的figure 边框才是绘制图形的地方,通过par 函数的oma 参数控制figure 区域的大小;

plot 区域绘制的是坐标系中的元素,在plot 区域和figure 边框之间的inner margin 用来放置坐标轴刻度,标题等元素,通过par 函数的mar 参数可以设置其大小

到此这篇关于R语言绘图布局实例讲解的文章就介绍到这了,更多相关R语言绘图布局内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!

本文标题为:R语言绘图布局实例讲解