Skip to content

scale_linetype_manual: Merging legends is difficult #4473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ptoche opened this issue May 8, 2021 · 2 comments
Closed

scale_linetype_manual: Merging legends is difficult #4473

ptoche opened this issue May 8, 2021 · 2 comments

Comments

@ptoche
Copy link

ptoche commented May 8, 2021

I'm finding it difficult to merge a scale_linetype_manual with the other guides.

  1. I'm incompetent or tired or both
  2. The docs could have one or two examples of usage for scale_linetype_manual (I don't see any at this time)
library("ggplot2")

df <- structure(list(year = c(1990, 2000, 2010, 2020, 2030, 2040), 
                     variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Something", class = "factor"), 
                     value = c(4, 5, 6, 7, 8, 9), category = structure(c(1L, 1L, 
                                                                         1L, 2L, 2L, 2L), .Label = c("Observed", "Projected"), class = "factor"), 
                     category2 = structure(c(NA, NA, 1L, 1L, NA, NA), .Label = "Transition", class = "factor")), row.names = c(NA, 
                                                                                                                               6L), class = "data.frame")

df.sub <- na.omit(df)

# First attempt:
ggplot(data = df, aes(x = year, y = value)) + 
    geom_point(aes(group = category, colour = category, shape = category), size = 4) +
    geom_line(aes(group = category, colour = category), size = 1) +
    geom_line(data = df.sub, aes(linetype = ""), 
              group = 1, colour = "black", size = 1) +
    scale_linetype_manual(name = "category", values = "dotted", labels = "Transition")

# Second attempt:
ggplot(data = df, aes(x = year, y = value)) + 
    geom_point(aes(group = category, colour = category, shape = category), size = 4) +
    geom_line(aes(group = category, colour = category, linetype = ""), size = 1) + 
    geom_line(data = df.sub, aes(linetype = category2), 
              group = 1, colour = "black", size = 1) + 
    scale_linetype_manual(name = "category", values = c("solid", "dashed", "solid"), labels = c("Observed", "Transition", "Projected"))

Created on 2021-05-07 by the reprex package (v2.0.0)

@thomasp85
Copy link
Member

You are right that merging manual legends are pretty difficult. The key is to make sure that all scales have the exact same title and breaks....

Down the line it would be nice to have a more flexible guide system but this is a pretty big effort unrelated to this particular issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants