Home > OS >  Change order of dates in R ggplot?
Change order of dates in R ggplot?

Time:06-24

I have been working on a plot in R using ggplot and plotting dates on the x axis. I have noticed that R does not recognize them as dates, and so the order on the x axis is wrong. I have tried many different things such as using as.Date(), manually editing levels and ordering the x axis, but nothing has worked. Here's my code:

library(dplyr)
library(ggplot2)
library(hrbrthemes)

calories_data = read.csv('dailyCalories_clean.csv',header = TRUE, sep=",")

ggplot(calories_data, aes(x= ActivityDay, y=Calories, group=Id, color = Id)) 
  geom_line()

Here's the plot

I appreciate any help, I'm new at this and have been researching for hours with no success. Thank you!

CodePudding user response:

One option to fix your issue would be to convert your dates to proper dates to fix the order and use the date_labels argument of scale_x_date to format your dates. To convert to dates you have to add a fake year to your ActivityDay, e.g. "2022":

Using some fake random data to mimic your real data:

library(ggplot2)

set.seed(123)

calories_data <- data.frame(
  ActivityDay <- rep(c("4/1", "4/10", "5/11", "5/1"), 3),
  Id = rep(1:3, each = 4),
  Calories = runif(12, 1000, 3000)
)

calories_data$ActivityDay <- as.Date(paste("2022", calories_data$ActivityDay, sep = "/"), format = "%Y/%m/%d")

ggplot(calories_data, aes(x= ActivityDay, y=Calories, group=Id, color = Id)) 
  geom_line()  
  scale_x_date(date_breaks = "5 day", date_labels = "%m/%d")

CodePudding user response:

To get the correct date from as.Date, you could incorporate the year first per below. From your plot image, it looks like the dates are input in month/day character format.

library(tidyverse)

calories_data <- tribble(
  ~ActivityDay, ~Calories, ~Id,
  "5/1", 10, 1,
  "5/2", 20, 1,
  "5/10", 15, 2,
  "5/11", 30, 2
)

calories_data |> 
  mutate(ActivityDay = str_c("2022/", ActivityDay),
         ActivityDay = as.Date(ActivityDay)) |> 
  ggplot(aes(ActivityDay, Calories, group = Id, color = Id))  
  geom_line()

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

  • Related