Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot legend.box.background produces strange relics

Tags:

r

ggplot2

This is a bit harder to explain than my last question because the problem is not exactly reproducible.

I am producing legends for a couple of maps and am drawing a box around both legends since one has only 1 item (a line feature) and the others are discrete fills (a polygon feature). Using geom_sf to plot both.

I end up with a weird artefact that looks like part of the lines are drawn twice with just a slightly shifted position.

notice the bottom right corner!!!

I managed to produce a similar error with the iris dataset where legend.box.background is only partially drawn.

data(iris)

ggplot(iris)+theme_classic()+
  geom_point(aes(x=Petal.Length, y=Sepal.Length, color=Species, size=Sepal.Width))+
  scale_color_manual(name=NULL, values=c("red","green","blue") ,labels=c("supersupersupersuperlong", "test2", "test3"))+
  theme(legend.position=c(0.1,0.75),legend.box.background=element_rect(fill="white", color="black"), legend.spacing.y=unit(0,"cm"))



UPDATE

I noticed in my original example it had to do with text length, so I tried adding a space after some of the labels which changes the "arrangement" of the twice-drawn lines a little bit. But I can't find an arrangement of whitespace that makes it go away completely. Anyone know how to manually change the size of the legend.box.background. If not I will draw a geometric rectangle and call it quits.

like image 981
Gmichael Avatar asked Oct 19 '25 02:10

Gmichael


1 Answers

I think the problem here is that the legend.background (which is a white rectangle behind each component of your legend), is partially drawing over the line surrounding the legend.box, which is the rectangle surrounding the whole legend. You can simply remove the legend.background

For example, your plot goes from this:

ggplot(iris) + 
  theme_classic() +
  geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species, 
                 size = Sepal.Width)) +
  scale_color_manual(name = NULL, values = c("red", "green", "blue"),
                     labels = c("supersupersupersuperlong", "test2", "test3")) +
  theme(legend.position = c(0.1, 0.75),
        legend.box.background = element_rect(fill = "white", color = "black"), 
        legend.spacing.y = unit(0, "cm"))

enter image description here

To this:

ggplot(iris) + 
  theme_classic() +
  geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species, 
                 size = Sepal.Width)) +
  scale_color_manual(name = NULL, values = c("red", "green", "blue"),
                     labels = c("supersupersupersuperlong", "test2", "test3")) +
  theme(legend.position = c(0.1, 0.75),
        legend.background = element_blank(),
        legend.box.background = element_rect(fill = "white", color = "black"), 
        legend.spacing.y = unit(0, "cm"))

enter image description here

like image 158
Allan Cameron Avatar answered Oct 20 '25 18:10

Allan Cameron