繁体   English   中英

更改图例中的线型

[英]Change linetypes in legend

图中的图例没有相应的线型,我可以在我的代码中添加什么来改变它?

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)

在此处输入图像描述

为了说明一些虚构的数据,您需要aes()内的linetype

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()

reprex 包于 2022-06-12 创建 (v2.0.1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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