R Tutorial - How to plot multiple graphs in R

preview_player
Показать описание
This part will explain you how to plot multiple graphs using R.

We'll be working with the shop data set: it contains 27 observations of a company's sales figures in relation to the amount of money spent on advertising, the number of nearby competitors, the current inventory and the overall size of the district. Have a look at its structure.

Suppose now that we want to get a first idea of the correlation between the net sales, in the `sales` column, and the other 4 variables, `ads`, `comp`, `inv` and `size_dist`. We could build four separate plots, but it can be a good idea to create a grid of plots, to compare the correlations more easily.

The easiest way to do is, is by specifying the graphical parameter `mfrow` with the `par()` function. Remember from before that you could use the `par()` function to list all the parameters, like this:

But you can also use it to set graphical parameters. Let's set the `mfrow` parameter to a vector containing two times two, to tell R that you want to build 4 subplots on a 2 by 2 grid:

Nothing really happens. It'll become clear once you start building some plots though. Let's start with net sales versus ad spending:

The plot shows up in the top left corner. If you now also plot `sales` versus `comp`, ..., it appears next to it. Adding the third plot, the relation with the inventory, and the fourth plot, that shows the relation with the size of the district, we end up with four figures:

Pretty cool huh! You saw here that the plots got added in a row-wise fashion. If you instead specify the graphical parameter `mfcol`, instead of `mfrow`, with the same specification, ..., and do the four plots again, one by one, you'll see that the graphs are added in a column-wise fashion. This vector c(2,2) denotes that you want to have a layout of two rows and two columns, in this order.

To reset the graphical parameters such that R plots a single figure per layout, you can set either mfrow or mfcol to a vector that denotes that you want a 1 by 1 grid:

Apart from specifying the mfrow or mfcol parameters using the `par()` function, there's also the `layout()` function, that allows you to specify more complex plot arrangements. This function expects a matrix, in which you specify the location of the figures on the output device. Have a look at this matrix, `grid`, that specifies the locations that 3 figures can take:

If you pass this matrix to the `layout` function, and do three plot calls afterwards, ..., You'll see that the first figure spans the entire width of the first row of the layout. You can go pretty far with customizing your layouts, but that's something for more advanced visualization courses. To reset this layout, you can simply call this command to specify a 1 by 1 layout, or you can again use the mfcol or mfrow parameter to reset the grid

Resetting this layout afterwards, every time you have been adapting the graphical parameters, can be quite tedious. Another, often easier way is to copy the old graphical parameters to a new variable first, like this. If you then do your customizations, for example setting the overall color to red and make a plot, you can easily clean up afterwards by restoring the old graphical parameters. If you now the same plot again, the red color is gone again.

Apart from building layouts to show different plots next to each other, you can also choose to stack different graphical elements on top of each other, as if you are adding layers to the same plot. Let's see how this works with an example. Let's first try to plot the sales versus the advertisement spending with some basic customization:

Next, let's try to build a model that models sales based on advertisement spending with the `lm()` function. The `lm()` function returns an lm object, which contains the coefficients of the line representating the linear fit.

You can now use these coefficients to plot a straight line on the plot. The `abline()` function helps us out: This function can take the coefficients of a straight line as a vector, or as separate values.

The `lwd` argument stands for line width here. Instead of building a new plot with simply a line, the previous plot is kept, and the straight line is added to the plot. This is a great way of adding more information to your plot!
Рекомендации по теме
Комментарии
Автор

This channel is just awesome. Subscribed!

AjayBhardwaj-pl
Автор

if i have 9 plots and 3 in each row, is it possible for me to give names to each rows?

samratdahal
Автор

I want to use layout(matrix(..)) for 5 plots. 3 in the first row and 2 in the second row. Can you please explain, how I am supposed to define the matrix? Thank you :-)

Frosiiee
Автор

Excellent vid. Congratulations. Subscribed.

eduardouresticharre
Автор

Hi I have about 4 lines and one other function to plot on the same graph, do you have anything that can help me with this?

yousifsalam
Автор

Good night,


I try to plot multiple graphs in R (7 rows and 2 cols), but I found "Error in plot.new() : figure margins too large". How can I solve that?


Thanks.

JavierCastilloSierra
Автор

Hey I want to aggregate data by dat, but such that corresponding rainfall gives sum of rows not mean value

harshabhogle
Автор

I am getting this error with abline:
"Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet
"

how can I solve it? I already have a lm for 2 variables and a plot with these 2 variables

GaMuSiNoevr
Автор

The function par doesn't work with the ggplot2 package?

eduardoteubervonborries
Автор

Can you tell me how, if possible can I make this work with parallelplot? the functions par() and now layout() have been useless for me to have three parallelplots in the same chart.

pjortiz
Автор

como puedo sacar los puntos para hacer los corners de un campo de futbol

ComerSano-VivirSano
Автор

sir how to plot a graph which has X axis to right and Y axis downwards please help

anithah
Автор

How can we get the data for this type of practice?

avy
Автор

How can I create a plot inside (top right ) the big one??

wathiqzayed