I have a dataset that looks like this:
Month count
2009-01 12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386
I want to plot the data (months as x values and counts as y values). Since there are gaps in the data, I want to convert the Information for the Month into a date. I tried:
as.Date("2009-03", "%Y-%m")
But it did not work. Whats wrong? It seems that as.Date() requires also a day and is not able to set a standard value for the day? Which function solves my problem?
Try this. (Here we use
text=Linesto keep the example self contained but in reality we would replace it with the file name.)The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of
?plot.zoo.The zoo series,
z, that is created above has a"yearmon"time index and looks like this:"yearmon"can be used alone as well:Note:
"yearmon"class objects sort in calendar order.This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of
zto"Date"class:time(z) <- as.Date(time(z)).