0

Trying to scale the date on the x-axis of a time series plot so that it's more spaced out and readable. I have been using scale_x_date to accomplish this but keep getting the following error message:

Invalid input: date_trans works with objects of class Date only

This is occurring despite initially defining date as Date class in my dataset.

See example code below:

#Code Update  1 - August 29, 2020#

#load packages# 
library(dplyr)
library(ggplot2)
library(tidyr)
library(lubridate)
library(scales)

#read dataset#
data = read.csv("WQ_long.csv")   
  
#define variables#  
as.factor(parameter)
as.numeric(value)
#as.Date(date,format="%m/%d/%y")
date <- as.Date(date, format="%m/%d/%y")


#define subset of data to make graph# 
downstream = data %>%
  filter(site %in% c("US1", "DS5", "DS10"))%>%
  filter(parameter == "Al(d)")

#make plot #
plot = ggplot(data = downstream,
              aes(x = date, y = value))+
  geom_point(aes(color = site), size = 1.3)+
  labs(x = "Date", 
       y = "Dissolved Aluminum (mg/L)", 
       title = "Dissolved Aluminum") + 
  theme_bw()+ 
  scale_x_date(date_breaks = "5 days")
   
plot
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
mprobins
  • 9
  • 3
  • `scale_x_date(breaks = '5 days')` – Duck Aug 16 '20 at 17:42
  • 3
    Also, don't use `attach()`. It's a terrible habit that causes more harm than it helps. – Gregor Thomas Aug 16 '20 at 17:56
  • 2
    This `as.Date(date,format="%m/%d/%y")` does not update the variable. You need to reassign the value with this`date <- as.Date(date,format="%m/%d/%y")` – Dave2e Aug 16 '20 at 18:25
  • @Dave2e has the correct answer here. May want to post that, because it is definitely the reason OP is getting that error message. `as.Date(...)` will only display `date` as a Date class to your console - it does not assign `date` with that formatting without the assignment `<-` notation. – chemdork123 Aug 17 '20 at 13:13
  • Thanks all for you input. @Dave2e suggestion was applied but Iam still getting the same error message. Also edited last part of code to read "scale_x_date(date_breaks = "5 days")" – mprobins Aug 29 '20 at 16:08
  • 1
    Without seeing a sample of your data or your edited code, we can't provide much additional help. I suggest reading this post https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and asking a new question. – Dave2e Aug 29 '20 at 17:23
  • See "Code Update 1" in original question – mprobins Aug 29 '20 at 19:05
  • Are you sure your dataframe "downstream" is in the format that you expect it to be in? Also, look at my previous comment concerning reassigning variables. – Dave2e Aug 29 '20 at 22:09
  • We still don't have a [mcve] ... – Ben Bolker Aug 29 '20 at 23:08
  • You may want to try `data$date <- as.Date(data$date, format="%m/%d/%y")`, since I don't see `date` defined as an object in your environment. – Z.Lin Aug 30 '20 at 07:47
  • @Z.Lin Reassigning the date as you recommended was successful in solving the problem. Thank you! – mprobins Oct 18 '20 at 05:03

0 Answers0