Home > Back-end >  How to Reorder Dates in Chronological Order in ggplot2
How to Reorder Dates in Chronological Order in ggplot2

Time:10-06

I am trying to reorder the dates on my graph into chronological order. I am pulling the dates from a CSV that has the dates formatted as DD-MM-YYYY. Currently, the graph is created with the dates placed at random.

How can I reorder the dates into chronological order?

Here is the code I am using to produce the graph:

library(tidyverse)

ggplot(gym, aes(x=D1, y=B1)) 
  geom_point(size=2) 
  geom_smooth(method="lm", se=FALSE, color="orange") 
  theme_linedraw() 
  xlab("Date") 
  ylab("Weight (lbs)") 
  ggtitle("Weight (lbs) vs. Date") 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
  theme(plot.title = element_text(face="bold")) 
  theme(plot.title = element_text(hjust = 0.5))

CodePudding user response:

Change the D1 column to date class and that should fix the plot.

gym$D1 <- lubridate::dmy(gym$D1)
  •  Tags:  
  • r
  • Related