Home > Enterprise >  How to have 2 or more scale_linetype_manual
How to have 2 or more scale_linetype_manual

Time:10-05

enter image description here

If I try to have 2 separate legends for lines using 2 separate scale_linetype_manual, I will get the error message above.

But then, is it not possible to have 2 separate legends to 2 different set of line categories?

CodePudding user response:

library(ggplot2)
library(ggnewscale)

tibble() %>%
  ggplot(aes(Sepal.Length, Sepal.Width))  
    geom_line(data = head(iris), mapping = aes(linetype = Species))  
    scale_linetype_manual(values = c("virginica" = 2, "setosa" = 3))  
    new_scale("linetype")  
    geom_line(
        data = tail(iris) %>% mutate(Species2 = Species),
        mapping = aes(linetype = Species2)
    )  
    scale_linetype_manual(values = c("virginica" = 1))

enter image description here

  • Related