Hi! I am Mike Marin and in this video we'll talk about
how to add some descriptive text to a plot created in R.
Quite often one would like to enhance an existing plot
by adding some descriptive text to the plot. We will be working
with the Lung Capacity data that was introduced earlier
in this series of videos. I've already gone ahead and imported the data into R
and attached it. we will add some text to the plot
using the "text" and the "mtext" commands. To access the Help menu
you can type "help" and in brackets the name of the command you'd like hope for,
place a question mark in front of the command name or
you can type the command name over here in the help search window.
First let's go ahead and make a scatterplot of the Lung Capacity
versus the Age. For help producing a scatterplot
you can see my earlier video on producing scatterplots in R.
We will use the "plot" command and plot age
versus lungcap. We'll also give this plot a title
and we will call it "Scatterplot of LungCap vs Age"
I'm gonna set the "las" argument equal to 1
to have the values on the y-axis rotated.
Now let's calculate
Pearson's correlation between the age and the lung capacity.
To learn more about the "cor" command
and calculating correlations in R you can see my video on correlation in R
now let's go ahead and add some text to this plot
We will add some text showing the correlation between these two variables
We can add text using the "text" command.
I'm gonna set x equal to 5 and y equal to 11
These indicate the x and y coordinates to place the text at.
We can use the "label" argument
to type the text that we would like to have appearing in the plot
Here we are going to set "r=0.82"
to have Pearson's correlation appear there. You can note
that this text is centered at the x equals 5
and y equals 11 coordinates specified. we can use the
"adj" or adjustment argument to have the text start
or end at these xy coordinates; for example
if we set the "adj" argument equal to 0
we can see here that the text will start
at x equals 5 y equals 11. conversely if we set the "adj" argument
equal to 1
the text will finish at x equals 5 y equals 11.
Now let's reproduce this plot again
and this time we'll add the text
setting the X coordinate to be 3.5 the y coordinate to be 13
we'll set the adjustment argument equal to 0
to have the text starting at these xy coordinates
and we will have a label of "r=0.82"
to have Pearson's correlation appear on there
let's talk about how we can change the size
and the color of this text. I'm gonna reproduce that plot
We will put the text in the same location.
Here if we set the "cex" argument equal to 0.5 this will have the text appearing as 50%
of the default size and we can change the color
using the "col" argument. here I'll set the color
equal to 4 to have the color blue. we can also change the font
that we like this to appear in using the "font" argument
I am going to produce that plot and let's add that text
I am going to add the "cex" argument back to 1
to have it plotted at the default size and if we set the font
argument equal to 4 this will produce bold
and italicized font.
Now we may want to do other things to enhance this plot;
for example
we might like to add some lines to the plot and then label these.
let's go ahead and add a horizontal line
at the value for the mean lung capacity. Here we use the
"abline" argument.
if we set 'h' equal to the mean of the lung capacity
this is going to produce a horizontal line at the y value
for the mean lung capacity and I'll set the color argument equal to 2
to have this line red. We can make lines
thicker or thinner using the "lwd" argument
which specifies the line width. Here I'll set the
"lwd" argument equal to 2 to have the line width double
the default size
and finally
we can add in some text to label this line as the mean lung capacity
again we use the "text" argument. I'll set the x-coordinate to be 2.5
the y coordinate to be 8.5, we set the adjustment argument
equal to 0 to have the text starting at these xy coordinates
we'll set the label to be the "Mean Lung Cap"
I'm gonna set the "cex" argument
equal to 0.65 to have the text appearing at 65%
of the default size and will set the color argument equal to 2
again to have the text appear in red
Now, let's discuss one final thing: we may want to add text into the margins of the
plot rather than the body of the plot itself.
We can do this using the "mtext" command
let's go ahead and reproduce that scatterplot
and now let' s add some text into the margins.
Here we can use the "mtext" command to do so
we set the "text" argument equal to the text
we would like to have appearing in our plot
Here we are going to use "r=0.82" as we were using before
and we use the "side" argument to specify which margin
this text should go into. Setting "side" equal to 1
will place the text on the bottom of the plot. if we set the "side" argument
equal to 2 it places the text on the left side of the plot
Similarly setting the "side" argument to 3
will place the text on the top and setting it to 4 will place the text on the right.
Also as we saw before we can use the
"adj" or adjustment argument to specify where the text will align itself.
Let's take a look at placing some text on the bottom again
and we can see if we set the adjustment argument
equal to 1 this will place the text on the right side of the axis.
If we set the adjustment argument equal to 0
the text will be placed on the left side of the axis
and we can use any value between 0&1
to specify where along the axis the text should be placed
for example we set it to be 0.75
we can see that the text will appear 75 percent of the way
to the right margin, also like we saw before
we can change the color, the font, and the size of the text
using in the "col", "font" and the "cex" argument.
Let's take a look at that plot again
We are going to use the "mtext" argument; here we'll place that same
"r=0.82" in there
I am going to set the "side" equal to 3 to have the text appearing on the top of the plot
will set the adjustment argument equal to 1 to have the text
right-aligned,will set the color argument equal to 4
to have the text as blue, I'll set the "cex" argument equal to 1.25
to have the text appearing as 25 percent larger than the default size
and will set the font equal to 4 to have bold and italicized font.
You can think of many different times you may want to add some text or
annotations
into a plot to illuminate certain things
these commands should help you in doing so
Thanks for watching this video and make sure to check out my other instructional videos