1

Hi I have the plot below and there are 2 legends showing up. I have a question:

Currently the legend I added has 6 entries "A Value", "Ad value", "au value", "b", "bD Value" and "bU value". I really want to the legend to show only 4 entries

  • "A" and in the legend this should be a blue solid line like it already is
  • "bd & bu" and in the legend this should be a blue DASHED line ...not sure how to get this
  • "A" and in the legend this should be a red solid line like it already is
  • "ad & au" and in the legend this should be a red DASHED line...not sure how to implement this

Any thoughts?

enter image description here

d =  data.frame (title =        c(  rep(c("aU","A","ad"),2), rep(   c("bU","b","bD"),2 ) )  ,
               time = c(1,1,1,2,2,2,1,1,1,2,2,2)   ,
               value = c(10,8,4,9,7,3,5,3,1,4,2,0))

d
 ggplot(data=d , aes(x=time, y=value, 
                                group=title, 
                                colour = title,
                                linetype =title))+
     geom_line()  + geom_point() +


       scale_colour_manual( name = "Metric",  

                                  values = c( 
                                  A = "red", 
                                  ad = "red",
                                  aU = "red",
                                  b = "blue",
                                  bU ="blue",
                                  bD= "blue"),

                                  labels = c( 
                                  A = "A value", 
                                  ad = "Ad value", 
                                  aU = "au value",
                                  b = "b",
                                  bU ="bU vlaue",
                                  bD= "dD Value")
                            )+

    scale_linetype_manual(name = "Metric",
                                 values =c(
                                  A = "solid", 
                                  ad = "dashed", 
                                  aU = "dashed",
                                  b = "solid",
                                  bU ="dashed",
                                  bD= "dashed"),
                              labels = c( 
                                  A = "A value", 
                                  ad = "Ad value", 
                                  aU = "au value",
                                  b = "b",
                                  bU ="bU vlaue",
                                  bD= "dD Value")
                          )
user3022875
  • 8,598
  • 26
  • 103
  • 167

2 Answers2

2

The second legend is showing up because you changed the labels and title in just one of the two guides. If you change the other to match it (or better yet, change the title column to match the labels you want), the color and linetype will show up together in one legend.

Alternatively, if the groupings match up (i.e., if the "U" means the same thing in "A" and "B"), you could set the color on one of those and the linetype on the other (which appears to be what you are currently doing). Like this:

d2 <-
  tidyr::separate(d
                  , title
                  , c("group","subgroup")
                  , 1
                  , FALSE)

ggplot(data=d2
       , aes(x=time, y=value, 
             group=title, 
             colour = group,
             linetype =subgroup))+
  geom_line()  + geom_point()

You could then manually set colors and linetypes if you still wanted.

enter image description here

Or, for your current approach:

ggplot(data=d , aes(x=time, y=value, 
                    group=title, 
                    colour = title,
                    linetype =title))+
  geom_line()  + geom_point() +


  scale_colour_manual( 
                       values = c( 
                         A = "red", 
                         ad = "red",
                         aU = "red",
                         b = "blue",
                         bU ="blue",
                         bD= "blue")
  )+

  scale_linetype_manual(values =c(
    A = "solid", 
    ad = "dashed", 
    aU = "dashed",
    b = "solid",
    bU ="dashed",
    bD= "dashed")
  )

To set just two line types, you can use:

d2$forLabel <- ifelse(d2$subgroup == "", "Value", "Limit")


ggplot(data=d2
       , aes(x=time, y=value, 
             group=title, 
             colour = group,
             linetype =forLabel))+
  geom_line()  + geom_point() +
  scale_linetype_manual(values = c(Limit = "dashed"
                                   , Value = "solid"))

enter image description here

I'd recommend this instead of separate labels for everything (in particular, in case you add more groups later)

As another alternative, if you are displaying intervals, would you instead consider using a ribbon, instead of two lines?

d3 <-
  d2 %>%
  group_by(group,time) %>%
  summarise(min = min(value), max=max(value)
            , value = value[forLabel=="Value"])

d3 <-
  d2 %>%
  group_by(group,time) %>%
  summarise(min = min(value), max=max(value)
            , value = value[forLabel=="Value"])


ggplot(data=d3
       , aes(x=time, y=value, 
             color = group))+
  geom_line()  + geom_point() +
  geom_ribbon(aes(ymin = min, ymax = max
                  , fill = group
                  , color = NULL)
              , alpha = 0.2
              , color = NA) +
  theme_minimal()

enter image description here

Sumedh
  • 4,835
  • 2
  • 17
  • 32
Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • Thanks Mark, can you see the edited code. I'd like the legend to show only 4 lables. Any thoughts on that? Thanks again. – user3022875 Jul 15 '16 at 17:10
  • Add a new column to your data frame that recodes `title` by collapsing it from the six levels of `title` to just four levels. Then use that new variable for the color and linetype aesthetics. – eipi10 Jul 15 '16 at 17:29
  • How are readers supposed to differentiate between the BD and BU (or ad and au) lines in your plot? I think @eipi10 is right though: you likely need to generate a new variable that has the collapsing that you want to do. – Mark Peterson Jul 15 '16 at 19:03
  • the dashed lines are upper and lower bounds which the user will know. – user3022875 Jul 15 '16 at 19:27
  • Added versions that do just that, including an alternative to using the dashed lines. – Mark Peterson Jul 15 '16 at 20:31
0
d1 <- data.frame(title = c("aU","A","ad","bU","b","bD"), 
                title2 = c("aU & ad", "A", "aU & ad", 
                           "bU & bD", "b", "bU & bD"))

d2 <- merge(d, d1, by = "title")

ggplot(d2 , aes(x = time, y = value, group = title, 
                colour = title2, linetype = title2)) +
    geom_line() + geom_point() + 
    scale_colour_manual(name = "Metric", 
                        values = c("A" = "red", "aU & ad" = "red", 
                                   "bU & bD" = "blue", "b" = "blue")) +
    scale_linetype_manual(name = "Metric", 
                          values = c("A" = "solid", "aU & ad" = "dashed", 
                                      "bU & bD" = "dashed", "b" = "solid"))

enter image description here

You can use ifelse to create title2, I used merge because I feel its a bit cleaner

Sumedh
  • 4,835
  • 2
  • 17
  • 32