3

I have attempted anything I could find but to no avail.

I have replicated my issue using the built in dataset mtcars. I am making a scatter plot with bubble chart characteristics with ggplot2 in R. A few things in my code that don't work with this data set are a log scale and forcing the colors of the points, but unless the order of those is a factor I think I can deal with that later.

What happened:

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

Ideally I would like to fix this problem and make the dashed line display in a horizontal fashion 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: To solve the issue with the diagonal rectangles instead of circle points see Didzis Elferts solution below. Still looking for a way to make the legend line horizontal instead of on the diagonal like it is now. Code with Didzis Elferts fix added is 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)
Kate_Rock
  • 33
  • 3
  • if I put `linetype="dashed"` **outside** the `aes` and turn `show_guide` to F, then it all appears ok, but of course, the legend of the 1:1 line is gone. I don't know if that's of any help... – PavoDive Jul 16 '15 at 18:57
  • Yeah that's the basic way I have the figure now for at least looking okay. I just really think it is a weird issue, and can see needing to have similar figures in the future. – Kate_Rock Jul 16 '15 at 19:21

2 Answers2

1

A brute force way to get a horizontal line in your legend would be to add a horizontal line to the plot via geom_hline and base your legend off that layer instead of geom_abline.

Of course, because you don't actually want a horizontal line in your plot you'd then have to hide it some way. I came up with two hacky ways to do this. You could either put the line outside the y variable limits and set ylim or you could try to make the line blend into the panel background color. The gridlines may stop the second option from being reasonable.

Option 1, put geom_hline outside of the plot limits:

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, put geom_hline behind the 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)
aosmith
  • 34,856
  • 9
  • 84
  • 118
0

One way to remove lines from the colour and size legends is to set linetype=0 inside override.aes= for colour= and size=

 + guides(colour = guide_legend(override.aes = list(size=6,linetype=0)),
        size = guide_legend(override.aes=list(linetype=0)))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • It definitely works, but how can you make the line dashed? If I apply your solution the line becomes solid... – PavoDive Jul 16 '15 at 19:12
  • Great! That fixed it. When I put it together with the rest of the code, it looks dashed. Is there a way to change the orientation of the line in the legend icon? – Kate_Rock Jul 16 '15 at 19:25