R Tutorial - How to plot multiple graphs in R
260.9 هزار بار بازدید -
9 سال پیش
-
This part will explain you
This part will explain you how to plot multiple graphs using R.
Join DataCamp today, and start our interactive intro to R programming tutorial for free: https://www.datacamp.com/courses/free...
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!
Join DataCamp today, and start our interactive intro to R programming tutorial for free: https://www.datacamp.com/courses/free...
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!
9 سال پیش
در تاریخ 1394/09/13 منتشر شده
است.
260,992
بـار بازدید شده