R Tutorial - How to plot multiple graphs in R

Learn to arrange multiple plots in a grid using par() and layout(), then layer elements like regression lines and annotations onto single plots. The SHOP dataset shows scatter plots of sales against advertising, competitors, inventory, and district size; use abline() to add fitted lines and points() or text() to annotate.
Chapters
By now, you should know how to build and customize plots in R. That's already pretty nice. However, you might have noticed that all these examples were limited to the plotting of a single source of data. For example, a histogram of a dataframe column or a scatter plot of two variables. But what about a histogram next to a box plot in the same graphical window? Or a scatter plot with a linear regression line on top of that? In this last video, I'll give you a sneak peek into all the possibilities to create multiple plots or to add more information to your plots. We'll be working with the SHOP dataset. 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 four variables. Adds comp, inf, and sizedist. 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 this 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 graphical parameters like this. But you can also use it to set graphical parameters. Let's set the MFROW parameter to a vector containing 2 times 2 to tell R that you want to build four subplots on a 2x2 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. 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 C22 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 MF row or mfcol to a vector. That denotes that you want a 1x1 grid. If you now plot something Again, it looks like it did before. Apart from specifying the MFROW or MFCOL parameters using the PARF 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 locations that three 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 1x1 layout. Or you can again use the MFCOL or MF row parameters 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 customization, 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 do the same plot again, the red color is gone. 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, can you still tell what the meaning is of all these different parameters? Next, let's try to build a model that models sales based on advertisement spending with the ALM function. The ALM function returns an ALM object which contains the coefficients of the line representing the linear fit. You can now use these coefficients to plot a straight line on the plot. The EB line function helps us out. This function can take the coefficients of a straight line as a vector or as separate values. The LVD argument stands for line width here. Instead of building a new plot with simply a line, the previous plot is kept and a straight line is added to the plot. This is a great way of adding more information to your plot. Next to EB line, you can also use functions like points, segments, lines, and text to supercharge your plots. You'll learn all about them in the exercises. Most of them work out of the box only. For the lines function, it's important that you pass the data points in the right order. If we simply use lines on the ads and sales columns. The lines connecting the points will be all over the place. You have to use the order function to get a ranks vector, just like you did to order dataframes. If you now rebuild the plot which with the app line and the lines function that uses reshuffled data points, it looks much better. Notice from this last example that once you add something to your plot, such as the mess of lines going from left to right, you cannot remove it anymore. Plotting an R works like painting things on a canvas. You can't take it off again. If you want to remove elements, you'll have to start over with a plot generating function like plots and generally add more layers to it with the functions I've just introduced. With this new knowledge on how to build multiple plots on a grid, as well as to add different layers to the same plot, I think you're totally ready for the final set of exercises of this introduction to R course. Thanks for watching.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







