0

I'm having a problem with making dashed lines appear in the legend. couldn't make "scale_linetype_manual" work.

Thanks.

years = mydata$g2_year
ggplot(data=mydata, aes(x=years))+

geom_line(mapping=aes(y=g1_center, color="Centre"), size=1.5) +
  stat_smooth(aes(y = g1_center, color = "Poly.(Centre)"),method = "lm", formula = y ~ x + I(x^2), size = 1,se = FALSE, linetype = "dashed") +
  geom_line(mapping=aes(y=g1_periphery, color="Periphery"),size=1.5)+
  stat_smooth(aes(y = g1_periphery, color = "Poly.(Periphery)"),method = "lm", formula = y ~ x + I(x^2), size = 1,se = FALSE, linetype = "dashed") +
  
  scale_x_continuous("", labels = as.character(years), breaks = years)+
  scale_y_continuous("", breaks = c(-5:5))+
  theme_light()+
  theme(axis.text.x = element_text(angle = 90, vjust=0.5), legend.position="top", text = element_text(size = 11),
        legend.key.size =  unit(0.5, "in")) +
  scale_color_manual(name="",values=c("#E69F00","#56B4E9", "#E69F00","#56B4E9"))+
  scale_linetype_manual(values = c(1, 1, 2, 2))

image

  • 3
    Does this answer your question? [ggplot2: Dashed Line in legend](https://stackoverflow.com/questions/13572572/ggplot2-dashed-line-in-legend) – Zero Pancakes Feb 04 '21 at 11:03
  • you need to pivot your dataframe into the long format and call the col , linetype accordingly. You can't do it with your current format. See the linked question in comment – StupidWolf Feb 04 '21 at 13:08

0 Answers0