Search code examples
rggplot2scale

How to solve date issue while plotting using date_break command for the x axis level?


This is the head of my data and code I am using.While ploting I got Error: Invalid input: date_trans works with objects of class Date only. How to get rid of this?

> my_data
# A tibble: 3,722 x 19
   Year20              Year19              Year18              Year17              Year16              Year15             
   <dttm>              <dttm>              <dttm>              <dttm>              <dttm>              <dttm>             
 1 2020-03-01 00:00:00 2019-03-01 00:00:00 2018-03-01 00:00:00 2017-03-01 00:00:00 2016-03-01 00:00:00 2015-03-02 00:00:00
 2 2020-03-01 00:00:00 2019-03-01 00:00:00 2018-03-01 00:00:00 2017-03-01 00:00:00 2016-03-01 00:00:00 2015-03-03 00:00:00
 3 2020-03-01 00:00:00 2019-03-01 00:00:00 2018-03-01 00:00:00 2017-03-01 00:00:00 2016-03-01 00:00:00 2015-03-03 00:00:00

my_data <- read_excel("chhotanagpur.xlsx", sheet = "chhotanagpur")
my_data
my_data = as.data.frame(my_data)
f20 = as.data.frame(table(my_data$Year20))
f20$Var1 = as.Date(f20$Var1, "%Y-%m-%d")
f20$Var1 = format(f20$Var1, format="%m-%d")
library(ggplot2)
library(scales)
library(dplyr)
g <- ggplot() +
  geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1) +
  xlab('Date') +
  ylab('Cum_Freq')+
  scale_x_date(date_breaks="3 day", date_labels = "%d/%m")
plot(g)

Solution

  • the format function returns a string not a date object, which is why the scale_x_date can't deal with it. If you run str on your dataframe you can see the data type of each column. As an example, the first attempt to graph below will work, but the second will fail:

    df <- data.frame(date = as.Date(rep(Sys.time(), 10))) %>%
      mutate(date2 = format(date, format = "%m-%d"))
    
    str(df)
    
    'data.frame':   10 obs. of  2 variables:
     $ date : Date, format: "2020-07-23" "2020-07-23" "2020-07-23" "2020-07-23" ...
     $ date2: chr  "07-23" "07-23" "07-23" "07-23" ...
      
    df %>%
      ggplot(aes(date)) +
      geom_histogram() +
      scale_x_date()
    
    df %>%
      ggplot(aes(date2)) +
      geom_histogram() +
      scale_x_date()