Let's say that my data is the following
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"))
I have a plot with 3 lines and I define the color and type of this lines using scale_linetype_manual
and scale_color_manual
. The problem is that when I use both commands the line's names does not correspond to the correct model.
The code that i am using is the following:
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="")
The result is the following, and the problem is that the legend does not coincide with the data. I want to say that the red dotted line represent the data of MHAR-ReVar, the solid line represent the MHAR-ReCov data
Try this:
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="")
Created on 2021-04-06 by the reprex package (v1.0.0)