1

I have a 3 lines, one is dashed and others are solid. I have to add legend for dashed and solid lines. But the legend is not shown in the plot.

plot <- data_this_shop %>%
  ggplot(aes(x=id, y= price)) +
  geom_point(size = 1.5) +
  geom_line(aes(y=mean(price)), linetype = "dashed") +
  geom_line(data = data_other_shops, aes(x=id, y=lower_95), linetype = "solid") +
  geom_line(data = data_other_shops, aes(x=id, y=upper_95), linetype = "solid") +
  scale_linetype_manual(values = c("dashed", "solid"),
                        labels = c( "Mean price", "95% CI")) 

Can anyone help please?

learner
  • 196
  • 9
  • 2
    Try moving `linetype` to *inside* the `aes()` function. For example, `geom_line(aes(y = mean(price), linetype = "Mean price"))`. – teunbrand Nov 01 '22 at 06:17
  • 1
    As @teunbrand indicates: There is a rule of thumb to memorize: "What is inside `aes()` will get a legend. See here: – TarJae Nov 01 '22 at 06:20
  • @teunbrand It worked. If you add it as answer, I will accept it. Thanks :) – learner Nov 01 '22 at 06:21

0 Answers0