0

I am getting an error using ggplot and I'm not sure how to fix it. I'm using data that looks like this:

      Date sent.mean
1 14-03-01  3.000000
2 14-03-03  1.600000
3 14-03-04  3.000000
4 14-03-05  1.142857
5 14-03-06  2.625000
6 14-03-07  2.083333

The code I'm using to generate to generate the daily graph is:

ggplot(date.mean, aes(Date, sent.mean)) + geom_line() +
  scale_x_date(format = "%b-%Y") + xlab("") + ylab("Average Sentiment")

The error I am getting is:

Error in scale_x_date(format = "%b-%Y") : 
    unused argument (format = "%b-%Y")

Any help is greatly appreciated!

3
  • 1
    There's no argument format, but date_labels. So you prly want scale_x_date(date_labels = "%b-%Y") Commented Oct 13, 2016 at 22:24
  • Thank you @lukeA. I made that change and am now getting this error: Error: Invalid input: date_trans works with objects of class Date only. Do you know how i might fix that error? Commented Oct 13, 2016 at 23:03
  • See @alistaire 's answer: you have to make sure your Date column is recognized of type Date by R - I guess it's of type character of factor right now. To tell, you needed to provide a reproducible example ready to copy-paste-run. Commented Oct 13, 2016 at 23:16

1 Answer 1

1

First, you need to parse your dates:

df$Date <- as.Date(df$Date, '%y-%m-%d')

Then, the parameter is not actually called format, but date_labels, which you can find by reading ?scale_x_date. Cleaned up a little:

ggplot(df, aes(Date, sent.mean)) + 
    geom_line() +
    scale_x_date(NULL, date_labels = "%b-%Y") + 
    ylab("Average Sentiment")

plot with fixed axis labels

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

Comments

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.