Fundamentals of R
The course is part of this learning path
This module looks at how to plot data in R. It builds on the knowledge you have learned in the previous modules to show you how to use R to interpret your data.
The objectives of this module are to provide you with an understanding of:
- How to create a basic plot in R
- Various types of plots in R
- The different graphical parameters of plots in R
- How to layer a plot in R.
Aimed at all who wish to learn the R programming language.
No prior knowledge of R is assumed
Delegates should already be familiar with basic programming concepts such as variables, scope and functions
Experience of another scripting language such as Python or Perl would be an advantage
Understanding mathematical concepts will be beneficial
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
In order to understand the various, different types of plots we have available, let us start by understanding the Type argument and using Type set to L standing for line. Let me create an X, a vector for X which is a sequence ranging from minus four to plus four. And I can overlay a series of plots using either the Type L argument or the Lines function. Here, I have a Plot function of the Y using the tilde notation of X and I'm using the Type argument to create a red line, initially, of the COS function. After that, I can also overlay plots on top using the Lines function and inputting in, again, using tilde notation for different Y and X. I'm changing the color on each just to help us understand what is created. So by running this function in three lines, in one go, what I'm seeing on the screen is an updated layering of plots. In the first instance, I have the red graph, the red line using the Type L part of the Plot function. To understand the Plot function, we can use help help on the function and that brings up the Help window. And we can scroll down in this, and we can see there are various, different arguments that we can use, and various, different Types, and you can read this in your own time. But I'd like to just highlight a few points on the screen now, that are noteworthy from this. And as you can see, I've just jotted in the various different Types that I've picked up inside of here, and that includes P for points, L for lines, B for both, C for part of a line, et cetera. Now, if I was to go back into my Plots window and if I now take a different stance and try and explain the Type argument, let us draw various, different Types using these various, different things that I've highlighted on the screen: P for points, L for lines, B for both, and maybe even in the middle where we look for O for over plotted. Now, if I was to run this function here, I'm running the Plot function on the sine graph against X with a Type L, and I'm adding in the title lines. Just to reiterate, we've already done this up, above using the Type L for the red line for the COS function. But this time now, I'm choosing the Sine function. So here I have a very simple use of the Type being the first one at the top, here. Next, I can take the default which is points, P for points. So I can run that and update this and say, actually, instead of lines, I'd like to show you points. And let's take a look at what is the O for over plotted. So that is both. It's putting a line and a point at the same time. Now, let us try and understand what is going on when we talk about using the Lines function. So here, let me clear this out just to remove the history. Am I sure I would like to clear all the plots in the history? Yes. Now, if I use the Line type to create a plot, so I'm using L for line, and I can use lines on that and create a blue line on top. So I'm using the Tan function to help make this completely clear. So the Tan, I can create a new plot now, using Type P. So I'll update that. I can see that I've changed it. Instead of being a blue line, it's now a dotted line. Again, I can use the Line argument, or the Line function to create this with type O, as we saw which was both the circle overlaid with the line. And we can continue exploring the various, different arguments of the Type within the Lines function, or Type of the Plot function using H for histogram, which maybe looks a bit more interesting on the screen. Perhaps it's a bit hard to imagine what's going on inside of the top half of this graph and the bottom half of this graph. Let us try a different version of this. If I swap them round, I can see I've swapped the Tan and the Sine and the Tan and the Sine around just to help understand what's going on in this plot. Then the last part, if I was to clear the screen and type in Type N. Why would I ever need to require to use Type N? So if we go back to our Help pane, and we can see N is for no plotting. So that's the final Type on the Help window. The Plot creates a graphical device, but with no plotting. So let us understand what that is. So now, using our same X that we have, we can create a plot where I'm only intending to draw the axes without any points. So without any points, Type N, and I'd like to mention on the title that I am expecting only the axes. Now, as a concluding thought, I'd like to note why do we do this. It provides a window sized for the Sine graph without actually drawing the Sine graph, which might be useful if you knew, later on, that you had to use the COS function or the Sine function, or another function. It would help you understand what you're expecting in the future. You can use other graphical functions to layer plots on to of this, as we had done at the beginning. And you can create multiple overlays or multi-layered pitches. You can build up the lines in different colors. So everything we've done so far could have been started off with a nice, simple pre-sized window to help us, restrict us, in our output. And if we remember an issue we saw when we ran this function, in the fact that we couldn't see everything, because Tan is larger than the Sine function. So if we had thought about this and created a Type N plot it would have helped us understand what we are doing. It's always important to think before we create our plots.
Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.
Kunal has helped clients with early stage engagement and formed multi week training programme curriculum.
Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation.
Data Scientist at a credit management company; applied statistical analysis to distressed portfolios.
Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform.
Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data.
Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.