Data Visualization in R - Adding text to plots

The Importance of Plot Format and Explanatory Text in Data Visualization

When it comes to data visualization, two important aspects cannot be overstated: the plot format and the explanatory text. A good data visualization must have a plot format that is well matched to the data being displayed, as this will help to effectively communicate the message of the visualization. Additionally, sufficient explanatory text is essential to make it clear what we are looking at.

Explanatory text can be added to a plot in several different forms, including axis labels, titles, legends, and text added directly to the plot. Base graphics functions in R typically create axis labels by default, although these can be overridden through the arguments xlab() and ylab(), which allow us to provide our own x-axis label and y-axis label, respectively. Some base graphics functions also provide default titles, but these can again be overridden.

For example, in the plot on the left, the default title returned by the density function tells us that the plot was generated by this function using its default options and also gives the R specification for the variable whose density we're plotting. In contrast, the right-hand plot uses a default title that has been overridden by specifying the optional argument main(). It's worth noting that including the return character (backslash n) in this character string creates a two-line title.

Another important aspect of data visualization is text itself. The text() function allows us to add explanatory text to an existing plot, and to do so we must specify values for x and y, which are the coordinates on the plot where the text will appear, as well as labels, a character vector that specifies the text to be added by default. By default, the text is centered at the specified x-values, but we can modify this alignment using the optional argument adj(). The default value is adj=0.5 for centered text, but other values give different alignments.

For instance, if we set adj=0, left-justifies the text so that the string starts at x, and if we set adj=1, right-justifies the text so that the string ends at x. It's also possible to specify negative values making the text start some distance to the right of x or values greater than 1 making the text end some distance to the left of x.

The appearance of added text can be changed through optional arguments to the text() function. By default, the text is created horizontally across the page, but we can change this with the srt argument, which specifies the angle of orientation with respect to the horizontal axis. For example, if we specify srt=30 degrees (positive), the text in red angles upward. Conversely, specifying srt=-45 degrees makes the text in green angle downward.

The text() function also allows us to specify the color of the text with the col argument and change the text size with the cex argument. For instance, by specifying cex=1.2, we have specified the text to be 20 larger than normal. Other characteristics that can be modified in this example are the font set by the font argument. The default value is font=1 which specifies normal text, while font=2 specifies boldface, font=3 specifies italics, and font=4 specifies both boldface and italics.

Finally, we can also add a legend to our plot, which will be discussed in the next video. For now, let's summarize that two important aspects of good data visualization are first a plot format that is well matched to the data being displayed and second sufficient explanatory text to make it clear what we are looking at.

"WEBVTTKind: captionsLanguage: entwo important aspects of a good data visualization are first a plot format that is well matched to the data being displayed and second sufficient explanatory text to make it clear what we are looking at explanatory text can be added to a plot in several different forms including axis labels titles legends or text added to the plot itself base graphics functions in r typically create axis labels by default although these can be overridden through the arguments x lab that allow us to provide our own x-axis label and y-lab that allow us to provide our own y-axis label some base graphics functions also provide default titles but again these can be overridden as we see here the plot on the left uses the default title returned by the density function which tells us that the plot was generated by this function using its default options and also gives the r specification for the variable whose density we're plotting in the right hand plot this default title has been overridden by specifying the optional argument main note that by including the return character backslash n in this character string we are creating a two-line title like the lines and points functions text is a low-level graphics function that allows us to add explanatory text to an existing plot to do this we must specify values for x and y the coordinates on the plot where the text will appear and labels a character vector that specifies the text to be added by default the text added to the plot is centered at the specified x values but the optional argument adj can be used to modify this alignment the default value is adj equals 0.5 for centered text but other values give different alignments adj equals 0 left justifies the text so the string starts at x while adj equals one right justifies the text so the string ends at x these three options are illustrated here it is also possible to specify negative values making the text start some distance to the right of x or values greater than 1 making the text end some distance to the left of x finally here we see some of the ways the appearance of added text can be changed through optional arguments to the text function by default text is created horizontally across the page but this can be changed with the srt argument which specifies the angle of orientation with respect to the horizontal axis here the text in red angles upward as a result of specifying srt as the positive value 30 degrees while the text in green angles downward by specifying srt equals minus 45. these examples also show that we can specify the color of the text with the col argument and the red text shows that we can change the text size with the cex argument here by specifying cex equals 1.2 we have specified the text to be 20 larger than normal the other text characteristic that has been modified in this example is the font set by the font argument the default value is font equals one which specifies normal text while font equals two specifies boldface font equals three specifies italics and font equals four specifies both boldface and italics as noted we can also add a legend to our plot something that we will discuss in the next video for now ittwo important aspects of a good data visualization are first a plot format that is well matched to the data being displayed and second sufficient explanatory text to make it clear what we are looking at explanatory text can be added to a plot in several different forms including axis labels titles legends or text added to the plot itself base graphics functions in r typically create axis labels by default although these can be overridden through the arguments x lab that allow us to provide our own x-axis label and y-lab that allow us to provide our own y-axis label some base graphics functions also provide default titles but again these can be overridden as we see here the plot on the left uses the default title returned by the density function which tells us that the plot was generated by this function using its default options and also gives the r specification for the variable whose density we're plotting in the right hand plot this default title has been overridden by specifying the optional argument main note that by including the return character backslash n in this character string we are creating a two-line title like the lines and points functions text is a low-level graphics function that allows us to add explanatory text to an existing plot to do this we must specify values for x and y the coordinates on the plot where the text will appear and labels a character vector that specifies the text to be added by default the text added to the plot is centered at the specified x values but the optional argument adj can be used to modify this alignment the default value is adj equals 0.5 for centered text but other values give different alignments adj equals 0 left justifies the text so the string starts at x while adj equals one right justifies the text so the string ends at x these three options are illustrated here it is also possible to specify negative values making the text start some distance to the right of x or values greater than 1 making the text end some distance to the left of x finally here we see some of the ways the appearance of added text can be changed through optional arguments to the text function by default text is created horizontally across the page but this can be changed with the srt argument which specifies the angle of orientation with respect to the horizontal axis here the text in red angles upward as a result of specifying srt as the positive value 30 degrees while the text in green angles downward by specifying srt equals minus 45. these examples also show that we can specify the color of the text with the col argument and the red text shows that we can change the text size with the cex argument here by specifying cex equals 1.2 we have specified the text to be 20 larger than normal the other text characteristic that has been modified in this example is the font set by the font argument the default value is font equals one which specifies normal text while font equals two specifies boldface font equals three specifies italics and font equals four specifies both boldface and italics as noted we can also add a legend to our plot something that we will discuss in the next video for now it\n"