1

I have two dataframes with columns DateTime, Date and parameter. Both dfs have both the columns where the values for the column Date are same and the values for the column parameter are different. Both the dfs have hourly averaged value for the given parameter, lets say V1.

I want to plot the value for both V1 from both dfs together using ggplot2 with 5 days interval in the x-axis. As all the dates are coming in the axis, the plot is unreadable. Here is the code i am using

ggplot() +
geom_point(data=df1, aes(Date, V1), color='blue') +
geom_point(data=df2, aes(Date, V1), color='red') +
theme_bw() +
scale_x_date(breaks="10 days", limits= as.Date(c("2012-12-15", "2013-01-15")))

scale_x_date doesn't seem to work. It is giving the following error

Error: Invalid input: date_trans works with objects of class Date only

Any help will be appreciated. Thank you in advance.

Shreta Ghimire
  • 1,019
  • 2
  • 13
  • 27
  • 1
    Your question does not contain a [reproducible example](http://stackoverflow.com/q/5963269/4303162). It is therefore hard to understand your problem and give you an appropriate answer. Please make your data available (e.g. by using `dput()`) or use one of the example data sets in R. Also, add the minimal code required to reproduce your problem to your post. – Stibu Mar 28 '16 at 11:19
  • I had encountered similar error, while trying to add text annotations to my plot made by ggplot, with x-axis in date format. When I converted the date to numeric, I got rid of this error. So for your problem, I would suggest that you can try : ggplot() + geom_point(data=df1, aes(Date, V1), color='blue') + geom_point(data=df2, aes(Date, V1), color='red') + theme_bw() + scale_x_date(breaks="10 days", limits= as.numeric(as.Date(c("2012-12-15", "2013-01-15")))) – Kumar Manglam Mar 28 '16 at 12:07
  • @KumarManglam with the use of `as.numeric` i am getting error `Error: Invalid input: date_trans works with objects of class Date only`. – Shreta Ghimire Mar 28 '16 at 13:11
  • I looked into the documentation of `scale_x_date` function. I see that you are using breaks instead of date_breaks. I think if you replace the breaks parameter with date_breaks, your code should work. – Kumar Manglam Mar 28 '16 at 13:49
  • @KumarManglam I got the following error `Error in continuous_scale(aesthetics, "date", identity, breaks = breaks, : unused argument (date_breaks = "10 days")` – Shreta Ghimire Mar 29 '16 at 03:46
  • @ShretaGhimire It would be easier to provide a solution if you provide sample data for `df1` and `df2`. However, I think you are looking for is what Kumar Manglam was suggesting and here is a code snippet (i.e. the change to make): `scale_x_date(date_breaks="10 days", limits= as.Date(c("2012-12-15", "2013-01-15")))` – steveb Mar 29 '16 at 06:10

0 Answers0