Plotting multiple line

I am trying to plot multiple lines of OTC National outstanding amount, Gross Credit Exposure and Gross Market Value against date from 2007 to 2018, each year has two quarter, as data below in table.

Date GCE GMV NOA
6/30/2007 2.67 11.01 507.9
12/31/2007 3.26 15.65 585.926
6/30/2008 3.86 20.15 672.55
12/31/2008 5.01 34.94 598.14
6/30/2009 3.74 25.06 594.55
12/31/2009 3.52 21.33 603.89

I saved the data in excel file called OTCNotional (CSV) format

OTC<-read.csv(file.choose()) 
> OTC$Date<-as.Date(OTC$Date, format="%m/%d/%y")
> ggplot(OTC,aes(x=OTC$Date))+ 
+ geom_line(aes(y=OTC$GCE), color="darkred")+ 
+ geom_line(aes(y=OTC$GMV, color="black"), linetype="twodash")

How do I plot those three lines against dates , any help please.

mqH*��>�

Hi Descartes,

If I understand correctly, you want each column to be it's own line. In order to do this you will need to reshape your date a bit so that the column headers GCE, GMV, and NOA, are values in a single column.

To do this, I would use gather from tidyr. This takes data from a wide to long format. In the example below you can see i'm specifying a new column called metric from the column headers, and their respective values are stored in the new value column. The -Date means that the Date column will not be gathered into a new column.

  
library(tidyverse)
library(ggplot2)

otc <- read_delim(
"Date   GCE GMV NOA
6/30/2007   2.67    11.01   507.9
12/31/2007  3.26    15.65   585.926
6/30/2008   3.86    20.15   672.55
12/31/2008  5.01    34.94   598.14
6/30/2009   3.74    25.06   594.55
12/31/2009  3.52    21.33   603.89", delim = "\t")


otc %>% 
  # parse date using dplyr::mutate() mdy (month day year) from lubridate
  # https://r4ds.had.co.nz/dates-and-times.html
  mutate(Date = lubridate::mdy(Date)) %>% 
  gather(metric, value, -Date) %>% 
  ggplot(aes(Date, value, color = metric)) +
  geom_line()

Created on 2019-08-08 by the reprex package (v0.2.1)

Dear Josiah

Thanks very much for your help, I am bit new to r-programming, please bear with me, Below are my code:

> library(tidyverse) 
  > library(ggplot2) 
> library(lubridate) 
 otc<-read_delim(file.choose(),delim = "\t") 
 > otc %>% 
+ mutate(Date = lubridate::mdy(Date)) %>% 
+ gather(metric, value, -Date) %>% 
+ ggplot(aes(Date, value, color = metric)) + + geom_line()

Unfortunately I am getting an error which see to relate to dataframe, bellor is the error

Error in lapply(list(...), .num_to_date) : object 'Date' not found

Another question, with mutate function can I possible plot multiple plots using different geom_ function, e.g geom_bar, and another geom_line().

Thanks

One small suggestion that might help people figure out the answer to the question, is to break the longer pipe chains into segments, and figure out which line is causing the error.

For example, if the error is in the step otc %>% mutate(Date = lubridate::mdy(Date)), then it may be worthwhile to inspect the otc data frame itself -- did read_delim() read it correctly? Is it possible that otc is a data frame of 1 column with the name "Date GCE GMV NOA" ?

I find reading data into R confusing at times, so that type of error has happened to me in the past...

Hi @Descartes19!

Like @AJF suggested, I suspect this part isn't working at all. I'm guessing you tried this line in order to try to follow this bit from @josiah's answer?

This is understandably confusing when you're new, but that part of @josiah's answer wasn't meant to be copied directly. That bit just creates some example data so that the code in the answer would run (since we don't have access to your CSV file).

You should keep the CSV import code you had before, assuming it seems to be working :grin: A very simple way to check if the data got imported as you expect: after you run this line…

OTC<-read.csv(file.choose())

…try running both of these lines. The first will print a summary of the columns and their data types for OTC (your data frame of imported data), and the second will open up OTC in a spreadsheet-like view for you to inspect directly:

str(OTC)
View(OTC)

So to try @josiah's answer on your own data, you'd want something like this:

OTC <- read.csv(file.choose())
OTC %>% 
  mutate(Date = lubridate::mdy(Date)) %>% 
  gather(metric, value, -Date) %>% 
  ggplot(aes(Date, value, color = metric)) + 
    geom_line()

Do you mean you want both bars and lines on the same plot? Or do you want to make different plots from the same data? (Both are possible! :grinning:)

2 Likes

Thanks very much @jcblum, this worked very well. I have realized that plotting this is not well description, I instead want to plot NOA against Date as bars, and in the same graph plot GCE as percentage of NOA as line.

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