filmov
tv
R Tutorial - Customizing Your Plots In R
![preview_player](https://i.ytimg.com/vi/0MrYVzPxBIc/maxresdefault.jpg)
Показать описание
Understand how to customize your plots in R and make them more fancier.
The plots that I've shown to you in the previous video and the ones you've created yourself in the interactive exercises look quite nice, but there are certainly more things we can do to make the plots fancier and more informative. What about setting a title, or specifying the labels of the axes? All of this is possible from inside R. Throughout our experiments, we will be using the `mercury` data frame, thats lists pressure versus temperature measurements of mercury. It contains two variables: temperature and pressure. Let's start with a simple plot:
We can clearly see that the pressure rises dramatically once the temperature exceeds 200 degrees celsius. But this plot is still kind of dull, isn't it? Have a look at this code, that specifies a bunch of arguments inside the `plot()` function.
The result looks like this. Can you tell which arguments led to which changes in the plot? `xlab` and `ylab` changed the horizontal and vertical axis labels, respectively, while `main` specified the plot title. If you set the `type` argument to `o`, you will have both points and a line through these points on your plot. If you only want a line, you can use type = "l", which looks like this:
Finally, the `col` argument specifies the plot color.
Most of the arguments that are used here, such as `xlab`, `ylab`, `main` and `type`, are specified in the documentation of the `plot()` function. However, the `plot()` function also allows you to set a bunch of other graphical parameters. An example of such a graphical parameter is `col`, which specifies the color. But there are many others.
You can specify these graphical parameters straight inside the `plot()` function, as you did with `col`. In this case here, the graphical parameter only has an effect on this specific graph. If you now plot the same graph, without the col argument, the green color is not there anymore.
You can also inspect and control these same graphical parameters with the `par()` function. Typing question mark par opens up its documentation, with information on all the parameters that you can specify. Simply calling `par()` gives you the actual values of these parameters. You can also use `par()` with arguments to specify session-wide graphical parameters: Suppose you set the color to blue using the `par()` function, ..., and now create a plot.
It's blue. If you next create another plot, ..., the plot is still blue! That's because parameters specified with `par()` are maintained for different plotting operations. If you list all graphical parameters again and select the `col` element, ..., you'll see that indeed, it's still set to blue.
For the rest of this video, let me focus on some of the most important graphical parameters. I'll do this by adding arguments to the plot function, instead of using the par function for this.
Just as the col parameter has col dot variants for other elements in the plot, the cex parameter also has its cex dot variants.
The `lty` argument specifies the line type. a line type of 1 is a full line, and the types 2 to 6 are all different types of lines, like you can see here. And last but not least, we have the `pch` argument, which specifies a plot symbol for the points you are plotting. There are more than 35 different symbols for plotting, going from plusses and small octogonals to stars and hashtags.
Like I said, all of these arguments are just the tip of the iceberg. One of R's main powers is visualization and this is clear from the numerous ways in which you can make your plots ready for a report. Just make sure you don't overdo things; interpretability should be the main goal at all times!
The plots that I've shown to you in the previous video and the ones you've created yourself in the interactive exercises look quite nice, but there are certainly more things we can do to make the plots fancier and more informative. What about setting a title, or specifying the labels of the axes? All of this is possible from inside R. Throughout our experiments, we will be using the `mercury` data frame, thats lists pressure versus temperature measurements of mercury. It contains two variables: temperature and pressure. Let's start with a simple plot:
We can clearly see that the pressure rises dramatically once the temperature exceeds 200 degrees celsius. But this plot is still kind of dull, isn't it? Have a look at this code, that specifies a bunch of arguments inside the `plot()` function.
The result looks like this. Can you tell which arguments led to which changes in the plot? `xlab` and `ylab` changed the horizontal and vertical axis labels, respectively, while `main` specified the plot title. If you set the `type` argument to `o`, you will have both points and a line through these points on your plot. If you only want a line, you can use type = "l", which looks like this:
Finally, the `col` argument specifies the plot color.
Most of the arguments that are used here, such as `xlab`, `ylab`, `main` and `type`, are specified in the documentation of the `plot()` function. However, the `plot()` function also allows you to set a bunch of other graphical parameters. An example of such a graphical parameter is `col`, which specifies the color. But there are many others.
You can specify these graphical parameters straight inside the `plot()` function, as you did with `col`. In this case here, the graphical parameter only has an effect on this specific graph. If you now plot the same graph, without the col argument, the green color is not there anymore.
You can also inspect and control these same graphical parameters with the `par()` function. Typing question mark par opens up its documentation, with information on all the parameters that you can specify. Simply calling `par()` gives you the actual values of these parameters. You can also use `par()` with arguments to specify session-wide graphical parameters: Suppose you set the color to blue using the `par()` function, ..., and now create a plot.
It's blue. If you next create another plot, ..., the plot is still blue! That's because parameters specified with `par()` are maintained for different plotting operations. If you list all graphical parameters again and select the `col` element, ..., you'll see that indeed, it's still set to blue.
For the rest of this video, let me focus on some of the most important graphical parameters. I'll do this by adding arguments to the plot function, instead of using the par function for this.
Just as the col parameter has col dot variants for other elements in the plot, the cex parameter also has its cex dot variants.
The `lty` argument specifies the line type. a line type of 1 is a full line, and the types 2 to 6 are all different types of lines, like you can see here. And last but not least, we have the `pch` argument, which specifies a plot symbol for the points you are plotting. There are more than 35 different symbols for plotting, going from plusses and small octogonals to stars and hashtags.
Like I said, all of these arguments are just the tip of the iceberg. One of R's main powers is visualization and this is clear from the numerous ways in which you can make your plots ready for a report. Just make sure you don't overdo things; interpretability should be the main goal at all times!
Комментарии