Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting 3 graphs in a 2-1 layout in R

Tags:

plot

r

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!

like image 539
user54517 Avatar asked Jun 15 '14 16:06

user54517


People also ask

How do you put multiple graphs on one plot in R?

We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.

How do I arrange multiple graphs in R?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.

What does par Mfrow C 1 2 )) mean in R?

par(mfrow) mfrow – A vector of length 2, where the first argument specifies the number of rows and the second the number of columns of plots.

Can you have multiple graphs in one figure?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.


1 Answers

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)

like image 114
jlhoward Avatar answered Sep 21 '22 14:09

jlhoward