在 r 中使用 scale_linetype_manual 错误地显示名称

Incorrectly displayed names using scale_linetype_manual in r

假设我的数据如下

structure(list(Date_spill = structure(c(14821, 14821, 14821, 
14822, 14822, 14822, 14823, 14823, 14823, 14824, 14824, 14824, 
14825, 14825, 14825, 14826, 14826, 14826, 14827, 14827, 14827, 
14828, 14828, 14828, 14829, 14829, 14829, 14830, 14830, 14830
), class = "Date"), name = c("Total_Var.V2", "Total_Cov.V2", 
"Total_Semi.V2", "Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2", 
"Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2", "Total_Var.V2", 
"Total_Cov.V2", "Total_Semi.V2", "Total_Var.V2", "Total_Cov.V2", 
"Total_Semi.V2", "Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2", 
"Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2", "Total_Var.V2", 
"Total_Cov.V2", "Total_Semi.V2", "Total_Var.V2", "Total_Cov.V2", 
"Total_Semi.V2", "Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2"
), value = c(49.4546983682789, 51.9303086440996, 92.629674668831, 
49.4548705378623, 51.9568197311001, 92.6639221895955, 49.4050580381077, 
52.0759311138546, 92.6960916166521, 49.4105943879168, 52.0780444332815, 
92.7383532168068, 49.4138062869719, 52.1659343814577, 92.7264996022303, 
49.9540234458416, 52.7289455840081, 92.8136667785365, 49.9103140374395, 
52.8808260158253, 92.805208075033, 49.655075046784, 51.7281324622375, 
92.7607146196158, 49.6062909588621, 51.8989768324288, 92.7160066233307, 
49.3349721254447, 51.6577369988633, 92.6983797038236)), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))

我有一个包含 3 条线的图,我使用 scale_linetype_manualscale_color_manual 定义了这些线的颜色和类型。问题是当我同时使用这两个命令时,行的名称与正确的模型不对应。

我使用的代码如下:

library(tidyverse)
ggplot(Total, aes(x = Date_spill, y=value, colour = name, linetype = name)) +
  geom_line() + 
  theme_test() +
  scale_color_manual(values = c("darkseagreen3", "skyblue3", "firebrick3"), 
                     labels = c("MHAR-ReVar",
                                "MHAR-ReCov", 
                                "MSHAR")) +
  scale_linetype_manual(values = 1:3, 
                        labels = c("MHAR-ReVar",
                                   "MHAR-ReCov", 
                                   "MSHAR")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        legend.position = "bottom",
        legend.title = element_blank())+
  labs(x = "", y="")

结果如下,问题是图例与数据不符。我想说红色虚线代表MHAR-ReVar的数据,实线代表MHAR-ReCov数据

试试这个:

library(ggplot2)


ggplot(Total, aes(x = Date_spill, y=value, colour = name, linetype = name)) +
  geom_line() + 
  theme_test() +
  scale_color_manual(breaks = c("Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2"),
                     values = c( "firebrick3", "darkseagreen3", "skyblue3"), 
                     labels = c("MHAR-ReVar",
                                "MHAR-ReCov", 
                                "MSHAR")) +
  scale_linetype_manual(breaks = c("Total_Var.V2", "Total_Cov.V2", "Total_Semi.V2"),
                        values = c(2, 1, 3), 
                        labels = c("MHAR-ReVar",
                                   "MHAR-ReCov", 
                                   "MSHAR")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        legend.position = "bottom",
        legend.title = element_blank())+
  labs(x = "", y="")

reprex package (v1.0.0)

于 2021-04-06 创建