Hi, I am Mike Marin and in this video
we'll talk about how to add a legend to a plot created in R
quite often two or more groups of observations are displayed on a single plot
using different colours, different plotting characters
or different lines to identify each set. We will discuss how to add a legend to
identify these
we'll be working with the Lung Capacity data that was introduced earlier
in this series of videos. I've already imported the data into R
and attached it. We can add a legend to a plot
using the legend command. to access the help menu
you can type help and in brackets the name of the command you'd like help for
or simply type the command name in the help search window here
One of our earlier videos showed how to plot
multiple groups using different colors and/or plotting characters on the plot
we also have a video on subsetting data using square brackets
taking a look at these videos may be useful
in helping you to create the plot we'll create in this video
we will discuss adding a legend to a plot of lung capacity versus
age separated for smokers and non-smokers
now let's go ahead and produce that plot. Here we'll create a plot
plotting the age taking out those
who smoke equals to No for the non-smokers
will plot that against the lung capacity
again subsetting those who smoke
is No, we'll add a title to this plot
and we'll name it Lung Capacity versus Age
for Smoke and Non-Smoke
and finally we'll plot these using the colour 4
or blue. You can notice here
that the X&Y lables are not very nice! So let's go ahead and redo this plot
adding a nicer X&Y labels
Here, we can add an X label that is the Age
and we can add a y label that is the Lung Capacity
There, that looks a bit nicer!
Now let's go ahead and add the smokers to this plot. To do so
we use the points command to add additional points
to this plot; again we'll plot the Age
only for those whose smoking value is Yes
will plot this against the Lung Capacity
again only for those whose smoking value is Yes
then we'll use the colour 2 or red
now let's go ahead and add a legend
into this plot. We can do so using the legend command
here we use an X value of 3.5
and a Y value of 14, these are the XY coordinates
for the top left corner of the legend box the legend argument
indicates what we would like to be written as the names in our legend
here we will create a character vector,
of first being Non-Smoke and the second label being Smoke
we can then use the fill argument
this will tell R to create boxes beside those names
filling them with the colours 4 and then 2
to match the blue and red that we used in our plot.
Now, we can see that the plotting characters using these open circles
can be a bit difficult to see, so let's redo this plot
using different plotting characters. I'm gonna go ahead and resubmit that
plotting command
Although here we're gonna tell R to use the plotting character
number sixteen, the 16th plotting character; we can see that this is a
solid circle. let's go ahead and resubmit those points for the Smokers
and we'll use plotting character 17
and we can see this is a solid triangle . we can also have are legend
use these plotting characters instead of the boxes that we previously used.
to do so we can resubmit the legend command, we'll remove this
fill argument, we will replace it with the colour argument
letting R know the colours to use are four and
two or blue and then red, and in the legend
to use plotting characters of 16 and then 17
we can now see when we submit this command, our legend is now using
a blue sold circle and a red solid triangle
as the plotting characters in the legend
we can also have R create a legend that does not have a box around it
and this is my personal preference for adding a legend; let's go ahead and
resubmit that plot command again, add in those points
and we can see in the legend command
we have this argument bty or box type,
if we set this equal to the letter N this is going to be no box.
I personally think that this legend has a much cleaner look to it now.
there may be times when you have added lines to your plot
and you want the legend to use these lines instead of the plotting characters
let's take a look at doing that. I'm gonna go ahead and resubmit this plot command
again resubmit these points command and now
let's add some lines to this plot that'd be a smooth spline
fit to the data. we can add lines to an existing plot
using the lines command. here I'm gonna add
a smooth spline. in this video we're not talking about what splines are
I'm just showing a line that we've added to the plot. We will add a spline
through the Age for only those whose Smoking
is No and
the Lung Capacity again only for those whose smoking
is No. we'll add this line in
with the colour 4 or blue, now this line maybe a bit too thin
and if you'd like to make it thicker you can do so using the
LWD or line width argument, so within that command
we can add the LWD argument, and if we set this
equal to 3 it's gonna make a line three times the default width.
we could see that line is much thicker now. now let's go ahead and add the line
in for the smokers
we're gonna change the colour here to 2
or red, and we're gonna change these to be yes
to indicate a smoker
now let add a legend that uses lines
instead of the plotting characters. I'll bring up our previous legend command
and here what we're going to want to do is remove the plotting character argument
and we're going to replace that with the LTY
or line type argument, we'll set this equal to 1
it's can use line type one which is the default line type, this solid line that
we see
and as we saw before if we like the lines to be thicker
within the legend we can add this LWD
or line width argument into the legend command, let's put a line width of three
to have these lines three times the default width; and finally
in our plot we may also wish to use different line types
for each of the lines. let's take a look at doing this.
I am going to re-submit that plot command
resubmit those points again
we're gonna add
that line of the smooth spline
and here we can add the LTY argument or line type. I am going to use line type 2
we can see this is the dashed line and again let's submit the line for the
smoker or the red group
here we're going to use LTY or line type 3,
Now, let's go ahead and resubmit that legend command
and within this command where we have the line type argument
we're gonna make this equal to a vector with values 2
and 3 indicating legend to use line type 2
and then line type 3, there's many different reasons
you may wish to add a legend to a plot and these commands we've talked about
should help you in figuring out how to add a legend in the way that you would like
there are also other things about the legend that can be changed
you can explore the Help menu to learn a bit more about a few of these things
thanks for watching this video and make sure to check out my other instructional videos