Line plot with different line types in legends

I am trying to plot multiple line plots with different line types. The lines type can be seen in plot view but I am unable to visualize in legends. Kindly find my code below and data in given link:
https://docs.google.com/spreadsheets/d/19vMlgAS2bixehz-v28QXw7VgPdiD7u54/edit?usp=sharing&ouid=108208055491136780352&rtpof=true&sd=true

> data <-  read_excel("NF(45)_2040_2059.xlsx", sheet = 1)
> colnames(data)<- c("Date","Observed","RCM1","RCM2","RCM3","RCM4","RCM5","RCM6","RCM7","RCM8","RCM9","RCM10","RCM16","Ensemble")
> set.seed(1234)
> p1 <- data %>% 
>   mutate(Date = dmy(Date)) %>%
>   ggplot(aes(x=Date)) + geom_line(data= data,aes(y=data$RCM1,color="RCM1",linetype = "RCM1"),size=1)+ geom_line(data= data,aes(y=data$RCM2,color="RCM2",linetype = "RCM2"),size=1)+
>   geom_line(data= data,aes(y=data$RCM3,color="RCM3",linetype = "RCM3"),size=1)+ geom_line(data= data,aes(y=data$RCM4,color="RCM4",linetype = "RCM4"),size=1)+
>   geom_line(data= data,aes(y=data$RCM5,color="RCM5",linetype = "RCM5"),size=1)+ geom_line(data= data,aes(y=data$RCM6,color="RCM6",linetype = "RCM6"),size=1)+
>   geom_line(data= data,aes(y=data$RCM7,color="RCM7",linetype = "RCM7"),size=1)+ geom_line(data= data,aes(y=data$RCM8,color="RCM8",linetype = "RCM8"),size=1)+
>   geom_line(data= data,aes(y=data$RCM9,color="RCM9",linetype = "RCM9"),size=1)+ geom_line(data= data,aes(y=data$RCM10,color="RCM10",linetype = "RCM10"),size=1)+
>   geom_line(data= data,aes(y=data$RCM16,color="RCM16",linetype = "RCM16"),size=1)+  geom_line(data= data,aes(y=data$Ensemble,color="Ensemble",linetype = "Ensemble"),size=1.5)+
>   ggtitle("Precipitation STD(NF45)") +
>   labs(x="time",
>        y=" Runoff",
>        linetype = "Legends",
>        color = "Legends")+
>   scale_color_manual(name="Legends",breaks = c("Observed","RCM1","RCM2","RCM3","RCM4","RCM5","RCM6","RCM7","RCM8","RCM9","RCM10","RCM16","Ensemble"),values = c('RCM1' = 'black','RCM2' = 'red',"RCM3"= "lightsalmon2","RCM4"= "brown","RCM5"="cyan", 
>    "RCM6"="steelblue","RCM7"="dodgerblue4","RCM8"="limegreen",'RCM9' = 'goldenrod1','RCM10' = 'purple4',"RCM16"="burlywood4","Ensemble"="black"))+
>   scale_linetype_manual(breaks = c("Observed","RCM1","RCM2","RCM3","RCM4","RCM5","RCM6","RCM7","RCM8","RCM9","RCM10","RCM16","Ensemble"),
>     values = c('RCM1' = 'solid','RCM2' = 'dashed',"RCM3"= "solid","RCM4"= "dashed","RCM5"="solid","RCM6"="dashed","RCM7"="solid","RCM8"="dashed",'RCM9' = 'solid','RCM10' = 'dashed',"RCM16"="dashed","Ensemble"="solid"))+theme_bw()

Hello,

I think there's a lot that can maybe be refined here! Consider reshaping your data rather than copy-pasting over and over again:

library(tidyverse)

data <- read_csv("NF(45)_2040_2059.xlsx - Sheet1.csv") |> 
  mutate(Date = lubridate::dmy(Date)) |> # fix date
  rename_with(.fn = ~str_replace_all(.x, "([1-9]|[1-9][0-9])_RCM", "RCM\\1")) # rename columns
#> Rows: 182 Columns: 14
#> -- Column specification --------------------------------------------------------
#> Delimiter: ","
#> chr  (1): Date
#> dbl (13): Observed, 1_RCM, 2_RCM, 3_RCM, 4_RCM, 5_RCM, 6_RCM, 7_RCM, 8_RCM, ...
#> 
#> i Use `spec()` to retrieve the full column specification for this data.
#> i Specify the column types or set `show_col_types = FALSE` to quiet this message.

data |> 
  pivot_longer(contains("RCM")|contains("Ensemble"), names_to = "rcm") |>
  mutate(rcm = factor(rcm) |> fct_rev()) |> 
  ggplot(aes(x = Date, y = value)) +
  geom_line(aes(color = rcm, lty = rcm, size = rcm)) +
  labs(x="time",
       y=" Runoff",
       linetype = "Legends",
       color = "Legends") +
  scale_color_manual(
    name = "Legends",
    breaks = c("Observed","RCM1","RCM2","RCM3","RCM4","RCM5","RCM6","RCM7","RCM8","RCM9","RCM10","RCM16","Ensemble"),
    values = c('RCM1' = 'black','RCM2' = 'red',"RCM3" = "lightsalmon2","RCM4" = "brown","RCM5" = "cyan","RCM6" = "steelblue","RCM7" = "dodgerblue4","RCM8" = "limegreen",'RCM9' = 'goldenrod1','RCM10' = 'purple4',"RCM16" = "burlywood4","Ensemble" = "black")
  ) +
  scale_linetype_manual(
    breaks = c("Observed","RCM1","RCM2","RCM3","RCM4","RCM5","RCM6","RCM7","RCM8","RCM9","RCM10","RCM16","Ensemble"),
    values = c('RCM1' = 'solid','RCM2' = 'dashed',"RCM3"= "solid","RCM4"= "dashed","RCM5"="solid","RCM6"="dashed","RCM7"="solid","RCM8"="dashed",'RCM9' = 'solid','RCM10' = 'dashed',"RCM16"="dashed","Ensemble"="solid")
    ) +
  scale_size_manual(
    breaks = c("Observed","RCM1","RCM2","RCM3","RCM4","RCM5","RCM6","RCM7","RCM8","RCM9","RCM10","RCM16","Ensemble"),
    values = c('RCM1' = 1,'RCM2' = 1,"RCM3"= 1,"RCM4"= 1,"RCM5"=1,"RCM6"=1,"RCM7"=1,"RCM8"=1,'RCM9' = 1,'RCM10' = 1,"RCM16"=1,"Ensemble"=1.5)
  ) +
  guides(size = guide_none()) +
  theme_bw()

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

2 Likes

Dear Jack, Many thank. It solved my problem.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.