4

I have a problem with creating ggplot2 with multiple line guides. The issue I'm facing is how to add multiple guides for linetype. As you can conduct from the code, I'm adding group-coloured line and multiple hline/vline. geom_line has meaningful legend by default but I struggle with extra hline and vline.

ggplot2 default

library(ggplot2) 
library(ggnewscale) 
gg <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +     
    geom_line(aes(group=Species, color = Species)) +    
    facet_grid(.~Species)  
    
hor <- data.frame(yintercept = c(4, 5), guide = factor(c("h1", "h2")))   
vert <- data.frame(xintercept = 6, guide = factor("v1"))  

gg + 
  geom_hline(aes(yintercept = yintercept, colour = guide), data = hor, show.legend = TRUE, linetype = "solid") + 
  geom_vline(aes(xintercept = xintercept, colour = guide), data = vert, show.legend = TRUE, linetype = "solid") 


enter image description here


edit: Thanks to @Henrik's comment I was able to make a little progress on adding a new legend. It's almost ideal but I'm not able to change the orientation of the described lines

ggnewline with new_scale_color


gg +   
  new_scale_color() +   
  geom_hline(aes(yintercept = yintercept, colour = guide), data = hor, show.legend = TRUE, linetype = "solid") + 
  scale_color_manual(name="Horizontal lines", values = c("red","darkred")) + 
  new_scale_color() +
  geom_vline(aes(xintercept = xintercept, colour = guide), data = vert, show.legend = TRUE, linetype = "solid") + 
  scale_color_manual(name="Vertical lines", values = "green")

enter image description here

ggnewline with new_scale("linetype")

Also adding new_scale("linetype") along with guides didn't make a trick

gg +   
  new_scale("linetype") +
  geom_hline(aes(yintercept = yintercept, colour = guide), data = hor, show.legend = TRUE, linetype = "dashed") + 
  scale_linetype_manual(name="Horizontal lines", values = c("red","darkred")) + 
  guides(
    linetype = guide_legend(override.aes = list(
      color = c("red","darkred"),
      orientation = c("horizontal", "horizontal")
    ))
  ) +
  new_scale("linetype") +
  geom_vline(aes(xintercept = xintercept, colour = guide), data = vert, show.legend = TRUE, linetype = "dashed") + 
  scale_linetype_manual(name="Vertical lines", values = "green") +
  guides(
    linetype = guide_legend(override.aes = list(
      color = "green",
      orientation = "vertical"
    ))
  ) 

enter image description here


Expected

A desired result should have a guides with proper linetype orientation. Can be splitted in "horizontal" and "vertical" as ggnewscale does or combined.

enter image description here

GoGonzo
  • 2,637
  • 1
  • 18
  • 25
  • 2
    Check `ggnewscale::new_scale_color`. E.g. [How to make two legends using ggplot 2?](https://stackoverflow.com/questions/60309874/how-to-make-two-legends-using-ggplot-2/60311045#60311045), [ggplot: Separate legend for both a geom_vline and geom_hline](https://stackoverflow.com/questions/68394571/ggplot-separate-legend-for-both-a-geom-vline-and-geom-hline/68395253#68395253) – Henrik Dec 02 '21 at 09:01
  • Thank you @Henrik linked topics are really helpful. Still not ideal – GoGonzo Dec 02 '21 at 10:19
  • @GoGonzo Did you solve this question already? or do you still not have the excepted plot? – neuron Dec 02 '21 at 14:18
  • Hi @neuron not solved :/ Meanwhile, I have created an issue in the ggnewscale repo https://github.com/eliocamp/ggnewscale/issues/36 – GoGonzo Dec 02 '21 at 14:23
  • 2
    Have you tried with removing `show.legend = TRUE`? When I drop that I get your desired result with splitted legends. – stefan Dec 02 '21 at 15:22
  • 1
    @stefan works! Thank you! Feel free to add answer - I'll accept – GoGonzo Dec 02 '21 at 15:35

0 Answers0