rggplot2dplyrzooggfortify

How to input monthly data for time series plot in R?


I have data stored in .csv file which has two columns:

Date    mean
201001    234
201002    167
...
...
...
202005    289

(format is yyyymm)

I want to create a time series plot for the same. I am trying to do so using the following code in R:

library("zoo")
library("dplyr")
library("ggplot2")
library("ggfortify") 
alb_mean <- read.csv("D:/Monthtest/alb_mean.csv")
View(alb_mean)
ff <- zoo(alb_mean$mean, seq(from=as.Date("201001"), to=as.Date("202005", by=1)))

It gives me following error: Error in charToDate(x) : character string is not in a standard unambiguous format

Why it is giving me this error? Do I need to change the date format? Kindly help.


Solution

  • csv means comma separated values but there are no commas in the data shown in the question so we will create an actual csv file in the Note at the end and use that. First read it into a zoo object using read.csv.zoo -- if the data is in the format shown in the question as opposed to csv then use read.zoo(text = "lab_mean.csv", header = TRUE, FUN = to.ym) instead. In either case convert the first column to yearmon class which represents a year and month without a day. Then plot it using autoplot. See ?autoplot.zoo for further customizations.

    library(ggplot2)
    library(zoo)
    
    to.ym <- function(x) as.yearmon(as.character(x), "%Y%m")
    alb_mean <- read.csv.zoo("alb_mean.csv", FUN = to.ym)
    autoplot(alb_mean)
    

    screenshot

    ## Note
    
    Lines <- "Date    mean
    201001    234
    201002    167
    202005    289"
    Lines |>
      read.table(text = _, header = TRUE) |>
      write.csv("alb_mean.csv", row.names = FALSE, quote = FALSE)