简体   繁体   中英

Change linetypes in legend

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)

在此处输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM