Centzon Totochtin
Centzon Totochtin

Reputation: 167

Plotting monthly data with ggplot

I was trying to use ggplot to plot monthly data that looks like this

  count     date
      6 Oct 2010
     23 Nov 2010
     20 Dec 2010
     16 Jan 2011
     64 Oct 2011
     ...
    425 Jul 2012
    436 Aug 2012
    405 Sep 2012

where date is of yearmon class from the zoo package.

This was my call:

ggplot(data, aes(x=date, y=count))+geom_line()

and this error came up: Error: Discrete value supplied to continuous scale.

So ggplot doesn't support the yearmon class, which is fine.

Then I tried to convert yearmon to Date. Now the data looks like this:

 count       date
     6 2010-10-01
    23 2010-11-01
    20 2010-12-01
    16 2011-01-01
    64 2011-10-01
   ...
   425 2012-07-01
   436 2012-08-01
   405 2012-09-01

and I made the same call, and this was the resulting plot (sorry about the href... new users aren't allowed to post images)

There is a drop at the end of the plot that shouldn't be there, because data$count had the similar values on the last few rows.

Does anyone have a good solution to this?

Thanks for reading,

Bill

Upvotes: 5

Views: 6357

Answers (1)

bdemarest
bdemarest

Reputation: 14667

I am unable to recreate the problem with the line dropping unexpectedly on the right of the plot. Here is the code I used, and the output:

library(ggplot2)

dat = read.table(header=TRUE, colClasses=c("numeric", "Date"),
text=" count       date
     6 2010-10-01
    23 2010-11-01
    20 2010-12-01
    16 2011-01-01
    64 2011-10-01
   425 2012-07-01
   436 2012-08-01
   405 2012-09-01")

plot_1 = ggplot(dat, aes(x=date, y=count)) + geom_line()
ggsave("plot_1.png", plot_1, height=4.5, width=4.5)

enter image description here

You might consider posting your data before and after the date conversion (using dput()) to help people reproduce your problem.

Upvotes: 2

Related Questions