In ggplot2, legends give you valuable information about the mappings in a plot. But there are cases where you might be required to turn off some legends while keeping others. This can be done by controlling the guides, scales, and theme settings.
In this blog, we are going to explore different ways to selectively disable the legends in ggplot2.
Table of Contents
Turning Off Legends for Specific Aesthetics
Instead of removing all legends, you might want to disable legends for a specific aesthetic such as fill, color, or size.
1. Removing Legend for Fill
Now if you want to hide the legend for fill, set fill = NA in scale_fill_manual().
ggplot(df, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("red", "blue", "green"), guide = "none") # Removes fill legend
Output:
2. Removing Legend for Color
If you want to hide the legend for color, use scale_color_manual(guide = “none”).
ggplot(df, aes(x = category, y = value, color = category)) +
geom_point(size = 5) +
scale_color_manual(values = c("red", "blue", "green"), guide = "none") # Removes color legend
Output:
3. Removing Legend for Size
If your plot uses size as an aesthetic, you can disable its legend using scale_size_continuous(guide = “none”).
ggplot(df, aes(x = category, y = value, size = value)) +
geom_point() +
scale_size_continuous(guide = "none") # Removes size legend
Output:
Controlling Legends Using the guides() Function
Instead of using scale_*_manual(), you can use the guides() function for controlling which legends should appear.
ggplot(df, aes(x = category, y = value, color = category, size = value)) +
geom_point() +
guides(color = "none") # Removes only the color legend
Output:
Now,if you want to remove multiple legends at once:
ggplot(df, aes(x = category, y = value, color = category, size = value)) +
geom_point() +
guides(color = "none", size = "none") # Removes both color and size legends
Hiding Legends with theme()
A different way for hiding individual legends is by using the theme(legend.position = “none”), but for specific elements.
ggplot(df, aes(x = category, y = value, fill = category, color = category)) +
geom_bar(stat = "identity", position = "dodge") +
theme(
legend.title = element_blank(), # Removes legend title
legend.text = element_blank() # Removes legend text
)
Output:
Completely Removing All Legend
If you want to remove all the legends from a ggplot, you can use the theme(legend.position = “none”) option.
library(ggplot2)
# Sample Data
df <- data.frame(
category = c("A", "B", "C"),
value = c(10, 15, 20)
)
# Plot with No Legends
ggplot(df, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
theme(legend.position = "none") # Removes all legends
Output:
Conclusion
In ggplot2, there are several techniques to control the legend visibility. theme(legend.position = “none”) removes all the legends entirely. You can use guide = “none” within scale_*_manual() for removing specific legends. The guides() function can be used for fine-grained control over individual legend display. Moreover, theme() enables you to hide specific legend components, like title or text. By combining these different methods, you may adjust the legend appearance within a ggplot2 visualization.