Turning off some legends in a ggplot

Turning off some legends in a ggplot

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:

Removing Legend for Fill 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:

Removing Legend for Color 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:

Removing Legend for Size 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:

Controlling Legends Using the guides() Function 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
remove multiple legends at once Output.

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:

Hiding Legends with theme() 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:

Completely Removing All Legend Ouput

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.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner