0

I have data in excel that I would love to plot in chronological order, Unfornately it does appear in order.enter image description here

My code are here:

 library(tidyverse)
library(lubridate)
library(ggplot2)
OTCData <- file.choose()
OTC<- read.csv(OTCData,header=TRUE)
ggplot(data=OTC, aes(x=OTC$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=60, hjust=1))

The code produce the graph below which is not plotted in chronological order of date enter image description here

Please anyone to help out?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
user460329
  • 21
  • 3
  • Without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it is hard to check what is going wrong. But I guess, here the Date-column is turned into ```character```s - so something like ```OTC$Date <- as.Date(OTC$Date, format = " %m/%d/%y")``` should do. – Wolfgang Arnold May 06 '21 at 09:48

1 Answers1

0

I'm almost sure that the issue is that the DATE column is not being recognised as a date, but rather a character vector.

I would recommend that you reformat the DATE column. You're using tidyverse, so try:

OTC <- OTC%>%
mutate(DATE = as.Date(DATE, format = "%d/%m/%y")

(As an aside, this wouldn't even have been an issue if the dates in excel were stored in the iso format [i.e,yyyy-mm-dd]. It is an unambiguous format that can't mistake months for days, and in this case would've plotted correctly even if R saw those as characters and not dates)