Adding a legend entry makes all other legend entries diagonal and rectangular

I tried to find everything I could but to no avail.

I replicated my problem using the built-in mtcars dataset. I am doing a scatter plot with characteristics of a bubble plot with ggplot2 in R. Several things in my code that do not work with this dataset are log scale and forcing point colors, but if the order of these factors is not a factor, I think I can handle it later.

What happened:

I should have a 1: 1 line, I used geom_abline as it seemed to make the most sense. I used the methods I found from other questions to add a legend to a shape, but when that happens, all other legend entries change shape and orientation.

Ideally I would like to fix this issue and make the dotted line image horizontal if possible.

p <- ggplot(mtcars, aes(drat,wt, size=hp, colour=cyl)) +geom_point()+
   scale_size_area(max_size=5)+
   labs(x="drat", y="wt")
    p+ theme(panel.background = element_rect(fill = "white", colour = "black"), 
         panel.grid.major = element_line(colour= "grey75"), 
         axis.text.x = element_text(colour = "black"),
         axis.text.y = element_text(colour = "black"))+
   guides(colour = guide_legend(override.aes = list(size=6)))+
   geom_abline(aes(intercept = 0, slope = 1, linetype = "dashed"),show_guide = TRUE)+
   scale_linetype_manual("1:1 line", values = 2)

      

UPDATE: For a solution to the problem with diagonal rectangles instead of circle points, see Didzis Elferts' solution below. Still looking for a way to make the legend line horizontal and not diagonally as it is now. Code added by Didzis Elferts below

p <- ggplot(mtcars, aes(drat,wt, size=hp, colour=cyl)) +geom_point()+
   scale_size_area(max_size=5)+
   labs(x="drat", y="wt")
    p+ theme(panel.background = element_rect(fill = "white", colour = "black"), 
         panel.grid.major = element_line(colour= "grey75"), 
         axis.text.x = element_text(colour = "black"),
         axis.text.y = element_text(colour = "black"))+
   guides(colour = guide_legend(override.aes = list(size=6, 
    linetype=0)),size = guide_legend(override.aes=list(linetype=0)))+
geom_abline(aes(intercept = 0, slope = 1, linetype = "dashed"),
    show_guide = TRUE)+scale_linetype_manual("1:1 line", values = 2)

      

+3


source to share


2 answers


One way to remove strings from color and size legends is to set linetype=0

inside override.aes=

for colour=

andsize=



 + guides(colour = guide_legend(override.aes = list(size=6,linetype=0)),
        size = guide_legend(override.aes=list(linetype=0)))

      

0


source


Chances are, to get a horizontal line in your legend, you must add a horizontal line to the graphic through geom_hline

and create a legend from that layer instead geom_abline

.

Of course, because you don't really want a horizontal line in your plot, you'll have to hide it in some way. I came up with two hacky ways to do this. You can place the line outside of the y variable and set ylim

, or you can try to make the line blend to the background color of the panel. Grid lines can stop the second option from making sense.

Option 1, put geom_hline

outside of the graph:



ggplot(mtcars, aes(drat,wt, size=hp, colour=cyl)) +
    geom_point()+
    scale_size_area(max_size=5)+
    labs(x="drat", y="wt") + 
    theme(panel.background = element_rect(fill = "white", colour = "black"), 
        panel.grid.major = element_line(colour= "grey75"), 
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black")) +
    geom_abline(aes(intercept = 0, slope = 1), linetype = "dashed") +
    geom_hline(aes(yintercept = 0, linetype = "dashed"), show_guide = TRUE) +
    ylim(1.5, NA) +
    guides(colour = guide_legend(override.aes = list(size=6, linetype = 0)),
          size = guide_legend(override.aes = list(linetype = 0))) +
    scale_linetype_manual("1:1 line", values = 2)

      

Option 2, place geom_hline

behind other layers and make it white, use override.aes

to make it black in the legend:

ggplot(mtcars, aes(drat,wt, size=hp, colour=cyl)) +
    geom_hline(aes(yintercept = 3.2, linetype = "dashed"), show_guide = TRUE, color = "white") +
    geom_point()+
    scale_size_area(max_size=5)+
    labs(x="drat", y="wt") + 
    theme(panel.background = element_rect(fill = "white", colour = "black"), 
         panel.grid.major = element_line(colour= "grey75"), 
         axis.text.x = element_text(colour = "black"),
         axis.text.y = element_text(colour = "black")) +
    geom_abline(aes(intercept = 0, slope = 1), linetype = "dashed") +
    guides(colour = guide_legend(override.aes = list(size=6, linetype = 0)),
          size = guide_legend(override.aes = list(linetype = 0)),
          linetype = guide_legend(override.aes = list(colour = "black"))) +
    scale_linetype_manual("1:1 line", values = 2)

      

+1


source







All Articles