# Packages and global themelibrary(ggplot2)library(dplyr)
theme_set(theme_bw(base_size = 14) + theme(legend.title = element_text( face = "bold", size = 15, color = "turquoise3")))
<- iris %>% p ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length, shape = Species)) + geom_point(size = 4, alpha = .7, position = position_jitter(.1, .1, 123)) + scale_color_viridis_c(option = "B")
p
Remove Legend Titles in Two Distinct Ways in ggplot2
This tutorial explains how to remove legend titles in two distinct aspects:
Create a base plot
Remove the title of selected legend
Remove the title of the legend associated with the shape
aesthetic. labs()
is a quick solution to rename or remove legend titles (and axis titles as well).
+ labs(shape = NULL) p
Alternatively, use the scale_*
function to rename or remove legend (and axis) titles.
+ scale_shape_discrete(name = NULL) p
Another option is to use the guides()
function to rename or remove legend title. The following two codes are equivalent.
+ scale_shape_discrete(guide = guide_legend(title = NULL)) p + guides(shape = guide_legend(title = NULL)) p
Remove the titles of all legends.
+ theme(legend.title = element_blank()) p