layout, par(mfrow) - Combining Plots

R makes it easy to combine multiple plots into one overall graph, using either the
par( ) or layout( ) function.
With the par( ) function, you can include the option mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. mfcol=c(nrows, ncols) fills in the matrix by columns.

4 figures arranged in 2 rows and 2 columns
2行2列4图

attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")

image.png

3 figures arranged in 3 rows and 1 column
3行1列3图

attach(mtcars)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)

One figure in row 1 and two figures in row 2
上1图下2图

attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)

image.png

One figure in row 1 and two figures in row 2
row 1 is 1/3 the height of row 2
column 2 is 1/4 the width of the column 1

attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE),
widths=c(3,1), heights=c(1,2))
hist(wt)
hist(mpg)
hist(disp)

image.png

Add boxplots to a scatterplot

par(fig=c(0,0.8,0,0.8), new=TRUE)
plot(mtcars$wt, mtcars$mpg, xlab="Car Weight",
  ylab="Miles Per Gallon")
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天状态不好,但是学习R语言,貌似就是对着书本瞧瞧代码,瞧瞧代码,很适合打发时间,出来图形时候的微微成就感还是很有...
    生信要进步阅读 4,572评论 0 0
  • 3.1 使用图形 使用代码保存图形:保存为PDF—pdf(filename),其它格式:win.metafile(...
    壹亮3278阅读 7,488评论 0 53
  • 20171225(从有道迁移) 基本图形 条形图简单条形图:通过垂直的或水平的条形展示了类别型变量的分布(频数)语...
    KrisKC阅读 3,523评论 0 0
  • 这学期开了统计机器学习的课程,鉴于薄弱的概率论与统计学基础,学着还比较吃力,但是R语言的实践,还是令人兴趣十足。接...
    CharlesSun9阅读 10,175评论 1 6
  • 1.感恩老公早上跟我的对话,他说:“既然我都说了我没兴趣也不想去了解,你还总是在我面前提起,本来我对这个东西没有评...
    MayMay2018阅读 650评论 0 0