0

This is my minimal working example:

library(ggplot2)
library(dplyr)

n = 200
p = 10

df <- data.frame(
  x = 1:n/p,
  y = 2*sin(1:n/p) + 1:n/p + runif(n)) %>%
    mutate(a = rep(sum(y)/length(x), n))

ggplot(df, aes(x = x)) +
  geom_line(aes(y = y, fill = "LINE1")) +
  geom_line(aes(y = a, fill = "LINE2")) +
  geom_abline(aes(fill = "LINE3")) +
  theme_classic() +
  scale_colour_manual(values = c(
    "LINE1" = "red",
    "LINE2" = "blue",
    "LINE3" = "orange"))

enter image description here

Although I do get a plot, it does not have a legend. I also don't get colours. What am I doing wrong?

Xiphias
  • 4,468
  • 4
  • 28
  • 51
  • 2
    Use `color` not `fill` for line colors. `fill` is for areas (bars, ribbons, etc.). – Gregor Thomas Jul 26 '15 at 00:02
  • As to why you don't have a legend, it's because you're not mapping your color aesthetic to a single data column and using one `geom_line` call. You will need to reshape your data into long format using `reshape2::melt` or `tidyr::gather`. This is a commonly asked R-faq, duplicate here: http://stackoverflow.com/q/10349206/903061 – Gregor Thomas Jul 26 '15 at 00:05

0 Answers0