Draw ggplot2 Legend at the Bottom & with Two Rows in R (Example)


In this R tutorial you’ll learn how to move a ggplot2 legend with multiple rows to the bottom of a plot.

The article will consist of the following content:

Let’s just jump right in…


Example Data, Packages & Default Graphic

In the first place, we’ll have to define some example data:

data <- data.frame(x = 1:5,                        # Creating example data frame
                   y = 1:5,
                   group = LETTERS[1:5])
data                                               # Printing example data frame


table 1 data frame draw ggplot2 legend at bottom two rows r


Table 1 shows that the example data consists of five rows and three columns.

We also have to install and load the ggplot2 package, to be able to use the functions that are included in the package:

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

As next step, we can plot our data in a ggplot2 plot with default legend:

ggp <- ggplot(data, aes(x, y, color = group)) +    # Creating default ggplot2 plot
ggp                                                # Drawing default ggplot2 plot


r graph figure 1 draw ggplot2 legend at bottom two rows r


In Figure 1 you can see that we have created a ggplot2 graphic with default legend positioning on the right side of the plot.


Example: Move ggplot2 Legend to the Bottom with Two Rows

In this section, I’ll demonstrate how to create a ggplot2 legend at the bottom with multiple rows.

Let’s first use the theme function to move our legend to the bottom:

ggp +                                              # Moving ggplot2 legend to the bottom
  theme(legend.position = "bottom")


r graph figure 2 draw ggplot2 legend at bottom two rows r


By executing the previous R programming code we have created Figure 2, i.e. a ggplot2 graphic with a horizontally aligned legend below the plot.

If we want to draw a legend with multiple lines below our plot, we have to use the guides and guide_legend functions in addition to the theme function:

ggp +                                              # Specifying number of legend rows
  theme(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 2, byrow = TRUE))


r graph figure 3 draw ggplot2 legend at bottom two rows r


Figure 3 shows the output of the previously shown R syntax: A ggplot2 plot with a legend at the bottom of the plot and with multiple lines.

Note that we have used the byrow argument within the guide_legend function to order our legend by rows instead of by columns.


Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video instruction, I’m demonstrating the content of this article:


The YouTube video will be added soon.


In addition, you could have a look at the related tutorials of statisticsglobe.com. A selection of tutorials about ggplot2 plots can be found below.


In summary: You have learned in this tutorial how to create a ggplot2 graph legend with multiple rows wrapped at the bottom of a plot in the R programming language. Let me know in the comments, if you have additional questions.


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.