Adding a legend on multiple line plot

I want add legend on the code below and the excel data as shown below

Data

> library(tidyverse)
> library(lubridate)
> library(ggplot2)
> OTCData <- file.choose()
> OTC<- read.csv(OTCData,header=TRUE)
> 
> ggplot(data=OTC, aes(x=OTC$DATE, y=OTC$GrossCreditExposure))+
>   geom_line()+
>   geom_line(aes(X=DATE, y= GrossCreditExposure), 
>             , color = "blue",
>             alpha = 0.6,
>             size = 1,
>             group =1) +
>   geom_line(aes(X=DATE, y = GrossMarketValues), 
>             color = "red",
>             alpha = 0.6,
>             size = 1,
>             group =1) +
>   
>   labs(x="Date",y="Amount in US$", color = "Legend") +
>   scale_color_manual(values = colors)+
>   
>   ggtitle("OTC Derivative")+
>   theme(panel.background = element_rect(fill="white", colour="white", size=0.5, 
>                                         linetype="solid", color="black"),
>         plot.title = element_text(hjust = 0.5),
>         panel.grid.minor = element_line(size = (0.2), colour="grey"),
>         panel.grid.major = element_line(colour ="grey",size = (0.1)),
>         axis.text.x=element_text(angle=60, hjust=1),
>         legend.title = element_text(size=12, face="bold"))
````Preformatted text`

Please help

Excel uses data in a wide format to plot multiple series, with one column for each series, but ggplot likes a long format, with one column to labels the series and one column to hold all of the values. You can easily pivot your data from the wide format you have to a long format with the pivot_longer function from the tidyr package. ggplot will then automatically generate the legend when you make the color of the line depend on one of the columns.

library(ggplot2)
library(tidyr)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

Df <- data.frame(Date = c("2021-01-01", "2021-01-02", "2021-01-03"),
                 A = 1:3, B = c(2,1,3), C = c(3,2,1))
Df
#>         Date A B C
#> 1 2021-01-01 1 2 3
#> 2 2021-01-02 2 1 2
#> 3 2021-01-03 3 3 1

DfLng <- pivot_longer(Df, cols = A:C, names_to = "Source", values_to = "Value")
DfLng
#> # A tibble: 9 x 3
#>   Date       Source Value
#>   <chr>      <chr>  <dbl>
#> 1 2021-01-01 A          1
#> 2 2021-01-01 B          2
#> 3 2021-01-01 C          3
#> 4 2021-01-02 A          2
#> 5 2021-01-02 B          1
#> 6 2021-01-02 C          2
#> 7 2021-01-03 A          3
#> 8 2021-01-03 B          3
#> 9 2021-01-03 C          1

ggplot(DfLng, aes(Date, Value, group = Source, color = Source)) + geom_line()

Created on 2021-04-27 by the reprex package (v0.3.0)

Can I not set it Manual within the code?

you can also set the aestetics without making the data long and then use scale color manual

library(ggplot2)
Df <- data.frame(Date = c("2021-01-01", "2021-01-02", "2021-01-03"),
                 A = 1:3, B = c(2,1,3), C = c(3,2,1))
 ggplot(data=Df, aes(x=Date))+
   geom_line(aes( y = A, group =1L, color="A"))+
   geom_line(aes( y = B, group =1L, color="B"))+
   geom_line(aes( y = C, group =1L, color="C"))+
   scale_color_manual(values=c("blue","red","green"))

Created on 2021-04-27 by the reprex package (v2.0.0)

Now I am experiencing some problem, the legend is sorted however if you look at the pic of the graph belowderivative
The dates are not in exactly order, it plot for June for every year and jump to plot December for every year:

Instead of plotting 30/06/1998 then 30/12/1998 in that order.

you need to define the variable as a date using as.date or as.posix...
note that using this approach the order of the factors is alphabetical unless you specify the order you want with breaks.
in your case the red line legend should be above and the blue line below

library(ggplot2)
Df <- data.frame(Date = as.Date( c("1998-06-30", "1998-10-30", "1998-12-30")),
                 A = 1:3, B = c(2,1,3), C = c(3,2,1))
ggplot(data=Df, aes(x=Date))+
  geom_line(aes( y = A, group =1L, color="b.A"))+
  geom_line(aes( y = B, group =1L, color="a.B"))+
  geom_line(aes( y = C, group =1L, color="c.C"))+
  scale_color_manual(values=c("blue","red","green"),
                     breaks = c("b.A","a.B","c.C"))+
  scale_x_date(date_breaks = "2 months", date_labels = "%b")

Created on 2021-04-28 by the reprex package (v2.0.0)

Been trying this but is not working. Your Example seems easy with small number of rows: Let me say I have so many dates that I am reading from csv file not 3 (let me say 200), for example you used A=1:3 , entries for B are (2,1,3) and C are (3,2,1). What now happen if these entries are from csv files and they are so many, you cannot all mentioned them, it will be too much of work.

Was your date parsed correctly ?
Can you share excerpt or dummy data ?
If the data is not in the correct type then useless to talk about other things.
Make sure to use ready read_csv as it might have built in better detection of dates
Or use as.date with specified format if as.date cannot auto detect it

I am reading the file with read.csv, once add OTC$DATE=as.Date(OTC$DATE, format="%d/%m%/Y") I will get the following error Error in seq.int(0, to0 - from, by) : 'to' must be a finite number

these are some of dummy data form csv file.

DATE NotionalAmounts GrossCreditExposure GrossMarketValues
30/06/1998 72106521.77 1202805 2562152.803
31/12/1998 80276622.05 1328580 3209463.957
30/06/1999 81420274.61 1119372 2609693.198
31/12/1999 88156431.71 1023011 2793954.52
30/06/2000 93959822.42 936961 2554920.528
31/12/2000 95150854.68 1080349 3161552.443
30/06/2001 99648589.78 1019132 3041485.503
31/12/2001 111058769.9 1170902 3783434.112

and
here are the full codes

library(tidyverse)
library(lubridate)
library(ggplot2)
OTCData <- file.choose()
OTC<- read.csv(OTCData,header=TRUE)
OTC$DATE=as.Date(OTC$DATE, format="%d/%m%/Y")
ggplot(data=OTC, aes(x=DATE,y=OTC$NotionalAmounts))+
  geom_line()+
  geom_line(aes(x = DATE, y = NotionalAmounts), 
            color = "red",
            alpha = 0.6,
            size = 1,
            group =1) +
  labs(x="Date",y="Amount in US$")+
  ggtitle("Notional Amount Outstanding For OTC Derivatives For All Category")+
  theme(panel.background = element_rect(fill="white", colour="white", size=0.5, 
                                        linetype="solid", color="black"),
        plot.title = element_text(hjust = 0.5),
        panel.grid.minor = element_line(size = (0.2), colour="grey"),
        panel.grid.major = element_line(colour ="grey",size = (0.1)),
        axis.text.x=element_text(angle=90, hjust=1))

try to use the following format you had a typo and your date is probably NA
OTC$DATE=as.Date(OTC$DATE,ormat="%d/%m/%Y")
also please avoid using : aes(x=DATE,y=OTC$NotionalAmounts)
change to:
aes(x=DATE,y=NotionalAmounts)
your plot might be better with making the data long then facet:

please read the guidelines to make a reprex you can dput your data to make things easier for those who want to help you

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(ggplot2)

OTC <- structure(list(DATE = structure(c(10407, 10591, 10772, 10956, 
                                    11138, 11322, 11503, 11687), class = "Date"),
                 NotionalAmounts = c(72106521.77,80276622.05, 81420274.61,
                                     88156431.71, 93959822.42, 95150854.68,
                                     99648589.78, 111058769.9),
                 GrossCreditExposure = c(1202805L,
                                         1328580L, 1119372L, 1023011L,
                                         936961L, 1080349L, 1019132L, 1170902L),
                 GrossMarketValues = c(2562152.803, 3209463.957, 2609693.198,
                                       2793954.52, 2554920.528, 3161552.443, 3041485.503, 3783434.112)
                 ),
            row.names = c(NA, -8L), class = "data.frame")  
#OTC$DATE <-  as.Date(OTC$DATE,format="%d/%m/%Y")
OTC %>%
  gather(key,value,-DATE) %>% 
ggplot(data=.,
       aes(x=DATE,
           y=value))+
  facet_wrap(key ~ .,scales="free_y", ncol=1, strip.position = "top") +
  geom_line(aes(x = DATE, y = value,
                color = key), 
            alpha = 0.6, size = 1)+
  scale_y_continuous(labels = scales::label_dollar())

Created on 2021-04-29 by the reprex package (v2.0.0)

This topic was automatically closed 21 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.