eliocamp / ggnewscale

Multiple Fill, Color and Other Scales in `ggplot2`
https://eliocamp.github.io/ggnewscale/
GNU General Public License v3.0
406 stars 18 forks source link

Linetype guides got mixed when vertical and horizontal lines are combined #36

Closed gogonzo closed 2 years ago

gogonzo commented 2 years ago

Hi, I've discovered your package recently and it helped in recent issue of mine. I'm building a plot which combines some basic line and arbitrary straight lines. Each of the line should be described in the legend with appropriate color/type/orientation. It works excellent when the lines differs only by colour, otherwise it got little mixed.

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), horizontal = factor(c("h1", "h2")))   
vert <- data.frame(xintercept = 6, vertical = factor("v1"))  

gg +   
    new_scale_color() +   
    geom_hline(aes(yintercept = yintercept, colour = horizontal), 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 = vertical), data = vert, show.legend = TRUE, linetype = "solid") + 
    scale_color_manual(name="Vertical lines", values = "green") 

image


I have tried to fix it though the guides but it doesn't help

gg +   
    new_scale("linetype") +
    geom_hline(aes(yintercept = yintercept, colour = horizontal), 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 = vertical), 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"
      ))
    ) 

image

Do you have a plan to separate the other line-attributes?

Regards, DK

gogonzo commented 2 years ago

Closing the issue - I made a mistake by putting show.legend = TRUE - everything works as expected. You saved me!


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), horizontal = factor(c("h1", "h2")))   
vert <- data.frame(xintercept = 6, vertical = factor("v1"))  

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

image

eliocamp commented 2 years ago

I love when issues close themselves 🤣️. Glad everything's fixed now.