I'm trying to create a monthly time series in ggplot for time series analysis. This is my data:
rdata1 <- read_table2("date sales_revenue_incl_credit
2017-07 56,037.46
2017-08 38333.9
2017-09 48716.92
2017-10 65447.67
2017-11 134752.57
2017-12 116477.39
2018-01 78167.25
2018-02 75991.44
2018-03 42520.93
2018-04 70489.92
2018-05 121063.35
2018-06 76308.47
2018-07 118085.7
2018-08 96153.38
2018-09 82827.1
2018-10 109288.83
2018-11 145774.52
2018-12 141572.77
2019-01 123055.83
2019-02 104232.24
2019-03 435086.33
2019-04 74304.96
2019-05 117237.82
2019-06 82013.47
2019-07 99382.67
2019-08 138455.2
2019-09 97301.99
2019-10 137206.09
2019-11 109862.44
2019-12 118150.96
2020-01 140717.9
2020-02 127622.3
2020-03 134126.09")
I now use the below code to change the class of date and then plot with breaks and labels much easier using date_labels and date_breaks.
rdata1 %>%
mutate(date = ymd(date)) %>%
ggplot(aes(date, sales_revenue_incl_credit)) +
geom_line() +
scale_x_date(date_labels = "%b %Y", date_breaks = "1 month")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, vjust=0.5),
panel.grid.minor = element_blank())
I get the following error:
Error in seq.int(r1$mon, 12 * (to0$year - r1$year) + to0$mon, by) : 'from' must be a finite number

ymd()function didn't pick up your dates properly. Trymutate(date = ymd(paste0(date, "-01"))).ymd(rdata$date[1])and you'll see you getNAas the result. Even if you specify viaas.Date(rdata$date[1], format="%Y-%m")` it fails to work, since theDateformat needs to specify day too. The suggestion would be to just add "-01" to the end of each day in your column and thenymd()will work and so would theas.Date()function if you specifyformat="%Y-%m-%d").