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語言,貌似就是對著書本瞧瞧代碼,瞧瞧代碼,很適合打發時間,出來圖形時候的微微成就感還是很有...
    生信要進步閱讀 987評論 0 0
  • 3.1 使用圖形 使用代碼保存圖形:保存為PDF—pdf(filename),其它格式:win.metafile(...
    壹亮3278閱讀 2,600評論 0 53
  • 20171225(從有道遷移) 基本圖形 條形圖簡單條形圖:通過垂直的或水平的條形展示了類別型變量的分布(頻數)語...
    KrisKC閱讀 537評論 0 0
  • 這學期開了統計機器學習的課程,鑒于薄弱的概率論與統計學基礎,學著還比較吃力,但是R語言的實踐,還是令人興趣十足。接...
    CharlesSun9閱讀 4,734評論 1 6
  • 1.感恩老公早上跟我的對話,他說:“既然我都說了我沒興趣也不想去了解,你還總是在我面前提起,本來我對這個東西沒有評...
    MayMay2018閱讀 98評論 0 0