How can I remove a legend title in ggplot2? 2

How can I remove a legend title in ggplot2?

To remove a legend title in ggplot2, the user can simply add the argument “legend.title = NULL” within the “guides” function. This will remove the title from the legend without affecting any other elements in the plot. Additionally, the user can also use the “theme” function to modify the legend title and make it invisible by setting “legend.title = element_blank()”. Both of these methods will effectively remove the legend title in ggplot2.

Remove a Legend Title in ggplot2


You can use the following syntax to remove a legend title from a plot in ggplot2:

ggplot(df, aes(x=x_var, y=y_var, color=group_var)) +
  geom_point() +
  labs(color=NULL)

The argument color=NULL in the labs() function tells ggplot2 not to display any legend title.

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

Example: Remove Legend Title from Plot in ggplot2

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

df <- data.frame(assists=c(3, 4, 4, 3, 1, 5, 6, 7, 9),
                 points=c(14, 8, 8, 16, 3, 7, 17, 22, 26),
                 position=rep(c('Guard', 'Forward', 'Center'), times=3))

#view data frame
df

  assists points position
1       3     14    Guard
2       4      8  Forward
3       4      8   Center
4       3     16    Guard
5       1      3  Forward
6       5      7   Center
7       6     17    Guard
8       7     22  Forward
9       9     26   Center

If we use geom_point() to create a scatterplot in ggplot2, a legend will be shown with a title by default:

library(ggplot2)

#create scatter plot of assists vs. points, grouped by position
ggplot(df, aes(x=assists, y=points, color=position)) +
  geom_point(size=3)

Notice that the legend currently has the text “position” shown as the legend title.

To remove this title from the legend, we can use the labs(color=NULL) argument:

library(ggplot2)

#create scatter plot and remove legend title
ggplot(df, aes(x=assists, y=points, color=position)) +
  geom_point(size=3) +
  labs(color=NULL)

Notice that the legend title has been removed.

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

Cite this article

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

stats writer. "How can I remove a legend title in ggplot2?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-a-legend-title-in-ggplot2/.

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

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

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

stats writer. How can I remove a legend title in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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