2

First, sorry for posting without reproducible data. Hope you guys understand my question. This is my code. At the end of the code, I am trying to add abline. With the code, I am trying to add the name of abline to the legend but it does not work.enter image description here

ggplot(aes(x = week_id2, y = Index, color = Chain2, linetype = Chain2, group = Chain2), 
       data = data00 +
  geom_point(aes(shape=Chain2), size = 3) +  
  geom_line() + 
  scale_linetype_manual(values=c("twodash", "dashed", "dotted", "dotdash", "longdash")) + 
  scale_shape_manual(values=c(1:5)) +
  xlab("Week") + 
  ylab("Index") + 
  geom_hline(aes(yintercept=1)) 

As shown, I just simply add a name of the abline (let's say the name is "add") in the legend. How should I do it with my current code?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
John legend2
  • 840
  • 9
  • 18

1 Answers1

4

You can add either color or linetype to aes then use scale_color_xxx or scale_linetype_xxx to fine tune the legend. Here is an example using economics dataset

library(tidyverse)

df <- economics %>%
  select(date, psavert, uempmed) %>%
  gather(key = "variable", value = "value", -date)

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable), size = 1) + 
  geom_hline(aes(yintercept = 10, color = "My line")) +
  scale_color_brewer(palette = "Dark2", 
                     breaks = c("psavert", "uempmed", "My line")) +
  theme_minimal()

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable, linetype = variable), size = 1) + 
  geom_hline(aes(yintercept = 10, color = "My line", linetype = "My line")) +
  scale_color_brewer(palette = "Dark2", 
                     breaks = c("psavert", "uempmed", "My line")) +
  scale_linetype_manual(values = c("twodash", "dashed", "dotted"),
                     breaks = c("psavert", "uempmed", "My line")) +
  theme_minimal()

Edit: per OP's request, we separate linetype & color/shape legends

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable), size = 0.75) + 
  geom_point(aes(color = variable, shape = variable)) +
  geom_hline(aes(yintercept = 10, linetype = "My line")) +
  scale_color_brewer(palette = "Dark2", 
                     breaks = c("psavert", "uempmed")) +
  scale_linetype_manual("", values = c("twodash"),
                        breaks = c("My line")) +
  scale_shape_manual(values = c(17, 19)) +
  # Set legend order
  guides(colour = guide_legend(order = 1), 
         shape = guide_legend(order = 1),
         linetype = guide_legend(order = 2)) + 
  theme_classic() +
  # Move legends closer to each other 
  theme(legend.title = element_blank(), 
        legend.justification = "center", 
        legend.spacing = unit(0.1, "cm"), 
        legend.spacing.y = unit(0.05, "cm"), 
        legend.margin = margin(0, 0, 0, 0), 
        legend.box.margin = margin(0, 0, 0, 0))

Created on 2018-05-08 by the reprex package (v0.2.0).

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Each line is distinguished by not only line type but also point-type. And I only need to add a line to the legend for the abline (for the ""ADD" in the legend, there is no point type). Does your recommendation still work? – John legend2 May 08 '18 at 18:37