Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot legend.box.background produces strange relics

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading