Home > database >  ggplot legend without title leaves space for title
ggplot legend without title leaves space for title

Time:06-01

So I have a ggplot that doesn't require a legend because it actually has a title and thus doesn't need a legend that would simply repeat the title. Imagine something like this:

ggplot(iris) 
  geom_point(aes(x=Sepal.Length, y=Sepal.Width, color=Species)) 
  theme(legend.box.background=element_rect(fill="white", color="black")) 
  labs(color="") 
  ggtitle("Sepals ~ Species") 
  xlab("Length") 
  ylab("Width")

(ignore the fact that the legend in my reprex only has two lines drawn for the box)

Do you notice the graphical problem? Apparently ggplot "thinks" there is a legend title and leaves some space, so I though using element_blank for the legend title might work.

ggplot(iris) 
  geom_point(aes(x=Sepal.Length, y=Sepal.Width, color=Species)) 
  labs(color=element_blank()) 
  theme(legend.box.background=element_rect(fill=NA, color="black"),
        legend.margin=margin(t=0,r=0,b=0,l=0)) 
  ggtitle("Sepals ~ Species") 
  xlab("Length") 
  ylab("Width")

While this improves the situation by making the box smaller at the top, it does not fix the problem because the top space is still smaller. As I have manually set the legend margins to 0 this can't be the issue.

any ideas?

CodePudding user response:

You can set theme(legend.title = element_blank()) . This also means you don't need to set an empty string for the label.

To show this, let's make that box outline a little thicker, and use the "empty string" method:

ggplot(iris)  
  geom_point(aes(Sepal.Length, Sepal.Width, color = Species))  
  ggtitle("Sepals ~ Species")  
  labs(x = "Length", y = "Width", color = "")  
  theme(legend.box.background = element_rect(color ="black", size = 2))

enter image description here

We can see that there is an obvious space where the title should be.

But now let's try it with the element_blank() method:

ggplot(iris)  
  geom_point(aes(Sepal.Length, Sepal.Width, color = Species))  
  ggtitle("Sepals ~ Species")  
  labs(x = "Length", y = "Width")  
  theme(legend.box.background = element_rect(color ="black", size = 2),
        legend.title = element_blank())

enter image description here

As Tjebo points out, the other option is to use NULL instead of an empty string, which does the same thing as theme(legend.title = element_blank())

ggplot(iris)  
  geom_point(aes(Sepal.Length, Sepal.Width, color = Species))  
  ggtitle("Sepals ~ Species")  
  labs(x = "Length", y = "Width", color = NULL)  
  theme(legend.box.background = element_rect(color ="black", size = 2))

enter image description here

CodePudding user response:

You additionally need to change legend.spacing. Very related: Reduce padding in ggplot2 legend

By the way, margin() has as defaults all = 0, so you don't need to type them out... ;)

library(ggplot2)
ggplot(iris) 
  geom_point(aes(x=Sepal.Length, y=Sepal.Width, color=Species)) 
  labs(color=NULL)  
  theme(legend.box.background=element_rect(fill="white", color="black"), 
        legend.margin=margin(), 
        legend.spacing.y = unit(0, "mm"))

Created on 2022-05-31 by the reprex package (v2.0.1)

  • Related