How can I create a legend in ggplot2 with multiple rows? 2

How can I create a legend in ggplot2 with multiple rows?

To create a legend with multiple rows in ggplot2, first specify the desired number of rows using the “nrow” argument in the “guide_legend” function. Then, use the “guide_legend” function in the “scale_color_manual” or “scale_fill_manual” to customize the legend labels and colors. Finally, add the “theme” function and use the “legend.position” argument to adjust the legend’s position. This will allow for a neatly organized and visually appealing legend with multiple rows in ggplot2.

Create a Legend in ggplot2 with Multiple Rows


You can use the following syntax to create a legend in ggplot2 with multiple rows:

ggplot(df, aes(x=x_var, y=y_var, color=group_var)) +
  geom_point() +
  guides(color=guide_legend(nrow=2, byrow=TRUE)) 

The value for the nrow argument specifies the number of rows to use in the legend.

The following example shows how to use this syntax in practice.

Example: Create Legend in ggplot2 with Multiple Rows

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('Mavs', 'Heat', 'Nets', 'Lakers', 'Suns', 'Cavs'),
                 points=c(24, 20, 34, 39, 28, 29),
                 assists=c(5, 7, 6, 9, 12, 13))

#view data frame
df

    team points assists
1   Mavs     24       5
2   Heat     20       7
3   Nets     34       6
4 Lakers     39       9
5   Suns     28      12
6   Cavs     29      13

If we create a scatter plot in ggplot2 without specifying the number of rows to use in the legend, ggplot2 will place one label on each line by default:

library(ggplot2)

#create default scatterplot
ggplot(df, aes(x=assists, y=points, color=team)) +
  geom_point(size=3)

In order to create a legend with multiple rows, we must use the guides() function with the nrow argument:

library(ggplot2)

#create scatterplot with two rows in legend
ggplot(df, aes(x=assists, y=points, color=team)) +
  geom_point(size=3) +
  guides(color=guide_legend(nrow=2, byrow=TRUE)) 

Notice that the legend now has two rows.

If we’d like to change the location of the legend as well, we can use the theme() function with the legend.position argument:

library(ggplot2)

#create scatterplot with two rows in legend
ggplot(df, aes(x=assists, y=points, color=team)) +
  geom_point(size=3) +
  theme(legend.position='bottom') +
  guides(color=guide_legend(nrow=2, byrow=TRUE)) 

The following tutorials explain how to perform other common operations in ggplot2:

Cite this article

stats writer (2024). How can I create a legend in ggplot2 with multiple rows?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-legend-in-ggplot2-with-multiple-rows/

stats writer. "How can I create a legend in ggplot2 with multiple rows?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-legend-in-ggplot2-with-multiple-rows/.

stats writer. "How can I create a legend in ggplot2 with multiple rows?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-legend-in-ggplot2-with-multiple-rows/.

stats writer (2024) 'How can I create a legend in ggplot2 with multiple rows?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-legend-in-ggplot2-with-multiple-rows/.

[1] stats writer, "How can I create a legend in ggplot2 with multiple rows?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a legend in ggplot2 with multiple rows?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top