0
votes

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!

1
There's no argument format, but date_labels. So you prly want scale_x_date(date_labels = "%b-%Y")lukeA
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?tlev
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.lukeA

1 Answers

1
votes

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