2

I have a plot in which I would like to include a legend.

I have tried some solutions that were proffered to others, but the issue in my case was that I couldn't use my desired colours as the solutions required me placing colour within aes(), which produced different colours to the ones I specified.

I'm sure that the solution lies within scale_fill_manual, but I can't implement it correctly.

As such, how can I include a legend and keep the same line colours?

My data frame:

grouped <- structure(list(date = c("2018-07-16", "2018-07-17", "2018-07-18", 
"2018-07-19", "2018-07-20", "2018-07-21", "2018-07-22", "2018-07-23", 
"2018-07-24", "2018-07-25", "2018-07-26", "2018-07-27", "2018-07-28", 
"2018-07-29", "2018-07-30", "2018-07-31"), homepage_opens = c(5L, 
0L, 0L, 3L, 1L, 2L, 0L, 1L, 0L, 2L, 5L, 0L, 0L, 0L, 0L, 0L), 
    sitewide_opens = c(39L, 34L, 19L, 62L, 46L, 44L, 16L, 51L, 
    25L, 66L, 75L, 0L, 0L, 0L, 0L, 0L), chats_started = c(10L, 
    16L, 9L, 8L, 13L, 13L, 5L, 13L, 4L, 8L, 11L, 0L, 0L, 0L, 
    0L, 0L), chats_completed = c(7L, 13L, 8L, 4L, 5L, 9L, 6L, 
    13L, 2L, 7L, 5L, 0L, 0L, 0L, 0L, 0L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

My plot code:

ggplot(grouped) +
  geom_line(aes(x = date, y = sitewide_opens, group = 1),
            linetype = "dashed",
            colour = "forestgreen",
            alpha = 0.5) +
  geom_line(aes(x = date, y = homepage_opens, group = 1),
            colour = "blue") +
  geom_vline(aes(xintercept = 8),
             linetype = 2,
             colour = "black") +
  geom_text(aes(x = date, y = homepage_opens, label = homepage_opens),
            hjust = -0.15,
            vjust = -1.5,
            size = 3,colour = "black") +
  geom_text(aes(x = date, y = sitewide_opens, label = sitewide_opens),
            hjust = -0.15,
            vjust = -1.5,
            size = 3,
            colour = "black") +
  labs(title = "Title",
       x = "Date",
       y = "Count") +
  theme(plot.title = element_text(size = 25,
                                  face = "bold",
                                  colour = "black"),
        legend.position = "top",
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.text = element_text(colour = "black"))
Mus
  • 7,290
  • 24
  • 86
  • 130

2 Answers2

1

Whenever you use ggplot2, you should feed it long data. This makes it much easier to add more lines, text, or whatever (as @MusTheDataGuy already mentioned). Therefore, here a minimal ggplot2-style example (further, transform date to what it is...date - ggplot2 knows what to do with it and you don't need group):

grouped_long <- tidyr::gather(data = grouped, key = key, value = value, -date)

ggplot(data = subset(grouped_long, key %in% c("homepage_opens", "sitewide_opens")),
       aes(x = as.Date(date), 
           y = value, 
           label = value,
           color = key, 
           lty = key)) +
  geom_line() +
  geom_text(color = "black") +
  geom_vline(xintercept = 8, lty = 2) +
  scale_linetype_manual(values = c(sitewide_opens = 2,
                                   homepage_opens = 1)) + 
  scale_color_manual(values = c(sitewide_opens = "forestgreen",
                                homepage_opens = "blue"))
Tino
  • 2,091
  • 13
  • 15
0

You were almost right, you need to adjsut scale, but colour not fill (as you use colour to set color of line and fill to set what color is filling your object. Here is solution.

ggplot(grouped) +
  geom_line(aes(x = date, y = sitewide_opens, colour = "Sitewide opens",group = 1),
            linetype = "dashed",

            alpha = 0.5) +
  geom_line(aes(x = date, y = homepage_opens, group = 1,
                colour = "Homepage opens")) +
  geom_vline(aes(xintercept = 8),
             linetype = 2) +
  geom_text(aes(x = date, y = homepage_opens, label = homepage_opens),
            hjust = -0.15,
            vjust = -1.5,
            size = 3) +
  geom_text(aes(x = date, y = sitewide_opens, label = sitewide_opens),
            hjust = -0.15,
            vjust = -1.5,
            size = 3,colour = "black") +
  theme(legend.position = "top",
        legend.title=element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.text = element_text(colour = "black")) + 
  scale_colour_manual(values=c("Sitewide opens"="forestgreen", "Homepage opens"="blue"),                                                 labels=c("Sitewide opens", "Homepage opens"))

enter image description here

AlienDeg
  • 1,288
  • 1
  • 13
  • 23
  • Wouldn't reshaping the data be a bit more robust? – SCDCE Jul 27 '18 at 12:55
  • @AlienDeg Thank you, this works well but how can I remove the word `colour` from the legend and change the legend text from `sitewide_opens` to `Sitewide Opens`? – Mus Jul 27 '18 at 12:57
  • 1
    theme(legend.title=element_blank()) – SCDCE Jul 27 '18 at 13:02
  • @MusTheDataGuy adjusted code. – AlienDeg Jul 27 '18 at 13:07
  • @AlienDeg Thank you, this is good but the legend text isn't quite right as it contains an underscore; is it possible to change the text to read `Sitewide opens` and `Homepage opens` with no underscores? – Mus Jul 27 '18 at 13:09
  • Just change the aes color name to whatever you want then copy to the scale color name. – SCDCE Jul 27 '18 at 13:12
  • @MusTheDataGuy new code, new pic :) – AlienDeg Jul 27 '18 at 13:12
  • @AlienDeg Yes, this works perfectly. The labels were the wrong way around so I fixed that, but otherwise it's great. Thank you. – Mus Jul 27 '18 at 13:19