Combine Character String & Expressions in Plot Text in R (2 Examples)

 

This article explains how to add a text with expressions to a plot in the R programming language.

The content of the article is structured as follows:

Let’s jump right to the programming part.

 

Creation of Example Data

The following data will be used as a basis for this R programming tutorial:

data <- data.frame(x = 1:5,         # Create example data frame
                   y = c(1, 3, 5, 2, 7))
data                                # Print example data frame

 

table 1 data frame combine character string expressions text r

 

Have a look at the previous table. It shows that the example data contains five rows and two columns. The column x is an integer and the variable y is numerical.

 

Example 1: Combine Character String & Expressions in Base R Plot

This example illustrates how to draw a plot with text elements that contain expressions using Base R.

We can draw a Base R plot with a regular main title without expressions as shown below:

plot(data,                          # Draw plot without expressions
     main = "My Main Title")

 

r graph figure 1 combine character string expressions text r

 

If we want to add expressions to this main title, we can apply the expression() function:

plot(data,                          # Draw plot with expressions in main title
     main = expression("My Main Title"["No. 2"] ~ alpha ^ beta))

 

r graph figure 2 combine character string expressions text r

 

After running the previous R programming code the plot shown in Figure 2 has been drawn. As you can see, we have added subscripts, superscript, and Greek letters to our title.

 

Example 2: Combine Character String & Expressions in ggplot2 Plot

Example 2 illustrates how to combine character string and expression elements in a ggplot2 plot.

We first need to install and load the ggplot2 package, in order to use the functions that are contained in the package:

install.packages("ggplot2")         # Install & load ggplot2
library("ggplot2")

Now, we can draw a ggplot2 scatterplot using the code below:

ggp <- ggplot(data, aes(x, y)) +    # Create ggplot2 scatterplot
  geom_point()
ggp                                 # Draw ggplot2 scatterplot

 

r graph figure 3 combine character string expressions text r

 

In the next step, we can add a main title without expressions to our plot:

ggp +                               # Add main title without expressions
  ggtitle("My Main Title")

 

r graph figure 4 combine character string expressions text r

 

If we want to combine string and expression elements, we can use the expression() function once again:

ggp +                               # Add main title with expressions
  ggtitle(expression("My Main Title"["No. 2"] ~ alpha ^ beta))

 

r graph figure 5 combine character string expressions text r

 

In Figure 5 it is shown that we have plotted a ggplot2 scatterplot with subscripts, superscripts, and Greek symbols.

We may add such text and expression elements to other parts of our graph as well. The R syntax below shows how to use the annotate function to add a text with expressions inside the plotting area:

ggp +                               # Annotate text with expressions inside of plot
  annotate("text",
           x = 2,
           y = 4,
           label = expression("My Text"["No. 2"] ~ alpha ^ beta))

 

r graph figure 6 combine character string expressions text r

 

By executing the previous syntax, we have plotted Figure 6, i.e. a a ggplot plot with a text containing expressions.

 

Video, Further Resources & Summary

In case you need more information on the examples of this tutorial, I recommend watching the following video on my YouTube channel. I demonstrate the R programming code of this article in the video:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you could have a look at the related R tutorials on this website:

 

In this R tutorial you have learned how to annotate a text label with expressions to a plot. If you have further questions, kindly let me know in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top