Plotting 3 graphs in a 2-1 layout in R

2020-03-24 03:41发布

Is it possible to obtain 3 plots in a single figure in R with a distribution as shown in the image below? The plots must have the same width, and plot C should be centered.

-----   -----
| A |   | B |
-----   -----
    -----
    | C |
    -----

Thanks!

标签: r plot
1条回答
做自己的国王
2楼-- · 2020-03-24 04:13

Yes, with the layout(...) function.

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

So layout(...) takes a matrix where each element corresponds to a plot number. In this case, [1,1] corresponds to the first plot, [1,2] corresponds the the second plot, and [2,1:2] corresponds to the third plot.

This example is taken with slight modification from here.

If you want the bottom plot to be the same "width" as the two above, you can tweak the margins for that plot.

par(mar=c(4,4,2,2))
layout(matrix(c(1,2,3,3), 2, 2, byrow = TRUE))
hist(mtcars$wt)
hist(mtcars$mpg)
par(mar=c(2,14,2,14))
hist(mtcars$disp)

查看更多
登录 后发表回答