2

The legend in the graph does not have the corresponding linetypes, what can i add in my code to change that?

names(EV)
    library(ggplot2)
    library(reshape2)
    colors <- c("SK H" = "blue", "LV H" = "blue", "IE H" = "blue", "SK M" = "red", "LV M" = "red", "IE M" = "red")
    p3<-ggplot(EV, aes(x=Anos))
    p3 + geom_line(aes(y = SK.H, color = "SK H"), linetype="twodash",size=1.5)+
      geom_line(aes(y = LV.H, color = "LV H"), size=1.5)+
      geom_line(aes(y = IE.H, color = "IE H"), linetype="dotted",size=1.5)+
      geom_line(aes(y = SK.M, color = "SK M"), linetype="twodash",size=1.5)+
      geom_line(aes(y = LV.M, color = "LV M"), size=1.5)+
      geom_line(aes(y = IE.M, color = "IE M"), linetype="dotted",size=1.5)+
      ggtitle("EV between 2002 e 2019")+
      theme(plot.title = element_text(hjust = 0.5))+
      theme(plot.title=element_text(face="bold"))+
      labs(x = "Anos", y = "EV", color = "Legend") +
      scale_color_manual(values = colors)

enter image description here

CC-SAM
  • 41
  • 1
  • Please provide an example dataset, preferably using `dput` to aid with answering your question. – Mossa Jun 12 '22 at 19:40
  • In this [stackoverflow question](https://stackoverflow.com/questions/14875582/changing-the-line-type-in-the-ggplot-legend#:~:text=To%20use%20your%20original%20data,the%20type%20to%20variable%20name.&text=Then%20you%20should%20add%20scale_linetype_manual,set%20line%20types%20you%20need.) you can find a detailed answer of how to do that – bpvalderrama Jun 12 '22 at 20:07

1 Answers1

0

To illustrate with a little made-up data, you need linetype inside the aes():

library(tidyverse)

# Made-up data
tribble(
  ~x, ~y, ~type,
  1, 4, "a",
  2, 6, "a",
  1, 5, "b",
  2, 7, "b",
  1, 6, "c",
  2, 8, "c"
) |> 
  ggplot(aes(x, y, linetype = type)) +
  geom_line()

Created on 2022-06-12 by the reprex package (v2.0.1)

Carl
  • 4,232
  • 2
  • 12
  • 24