5

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for checking up on this. It did turn out to be a problem with the data on the SQL database. I didn't notice that some records were deleted some the October updates were bumped into the middle. Newbie mistake. Thanks for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.