Add Individual Text to Each Facet of ggplot2 Plot in R (Example)


In this tutorial you’ll learn how to annotate different text elements to each facet of a ggplot2 facet graph in the R programming language.

The tutorial will contain one example for the creation of ggplot2 plots. To be more precise, the article consists of these contents:

Let’s do this:


Example Data, Packages & Default Graphic

The data below will be used as basement for this R programming tutorial.

data <- data.frame(x = 1:9,                                 # Create example data
                   y = 9:1,
                   group = letters[1:3])
data                                                        # Print example data


table 1 data frame add individual text each facet ggplot2 r


As you can see based on Table 1, our example data is a data frame consisting of nine rows and three columns. The variables y and x contain the data points and the variable group defines the different facets.

We also have to install and load the ggplot2 package, to be able to use the corresponding functions:

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

As next step, we can plot our data in a default facet plot without any text elements:

ggp <- ggplot(data, aes(x, y, group = group)) +             # Create ggplot2 facet plot
  geom_point() +
  facet_grid(. ~ group)
ggp                                                         # Draw ggplot2 facet plot


r graph figure 1 add individual text each facet ggplot2 r


By running the previously shown code we have managed to create Figure 1, i.e. a facet plot without text elements using the ggplot2 package.


Example: Annotate Individual Text to Each Facet of ggplot2 Plot Using geom_text()

The following syntax explains how to add individual text elements to each panel of our ggplot2 facet graphic.

The difficulty of this is that the annotate function adds the same text element to every facet of our plot, if we do not explicitly specify something else:

ggp +                                                       # Add same text to all facets
           label = "My Text",
           x = 5,
           y = 5)


r graph figure 2 add individual text each facet ggplot2 r


The output of the previous R programming syntax is shown in Figure 2: A facet plot with the same text in each plot window.

If we want to draw a different text element on top of every facet, we first have to create a data frame that contains text labels, facet group names, and axis positions:

data_text <- data.frame(label = c("Text_1", "", "Text_3"),  # Create data for text
                        group = names(table(data$group)),
                        x = c(5, 0, 7),
                        y = c(5, 0, 3))
data_text                                                   # Print data for text


table 2 data frame add individual text each facet ggplot2 r


As shown in Table 2, we have constructed a data frame consisting of all relevant information for our facet plot by running the previously shown syntax.

As next step, we can apply the geom_text function to our text data to add individual text elements:

ggp +                                                       # Add individual text to plot
  geom_text(data = data_text,
            mapping = aes(x = x,
                          y = y,
                          label = label))


r graph figure 3 add individual text each facet ggplot2 r


The output of the previously shown code is illustrated in Figure 3 – A facet grid graph with different text elements in each facet panel.


Video & Further Resources

In case you need more information on the R programming codes of this tutorial, I recommend having a look at the following video of the Statistics Globe YouTube channel. I explain the R syntax of this tutorial 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.


Furthermore, you may have a look at the other tutorials on this website.


In summary: In this R article you have learned how to create individual text elements for each facet in a ggplot2 graphic. If you have further questions, please let me know in the comments section.


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.