Date_Trans Error

I have a plot of calculations that I am plotting over a geom_point plot. The calculations run over aprox 87 days. Each calculation has a date to follow. Because there would be too many values on the x-axis I used a piece of script to vary over a 4 day interval in the plot.

  scale_x_date(breaks = seq.Date(from = as.Date("2020-03-07"), 
                                 to = as.Date("2020-05-31"), by = 3)) +

Although I am getting an error saying date-trans does not work. I am unsure what this means.

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

#[1] 0.00000000 0.00000000 0.00000000 0.00000000 0.02422924 0.02211847 0.02030373 0.01806205 0.01584046 0.04314234 0.04064545 0.04090086
#[13] 0.03584243 0.03330357 0.04097595 0.03623013 0.03346147 0.03044403 0.02866031 0.02693319 0.02832876 0.02419024 0.02097880 0.02126051
#[25] 0.02057323 0.02002243 0.02200681 0.02515222 0.02629810 0.03094984 0.04723567 0.04598012 0.04485712 0.05394273 0.05373836 0.06080055
#[37] 0.06151201 0.06280606 0.07320772 0.07831162 0.11061757 0.11001137 0.11005805 0.11595313 0.12176875 0.12839616 0.12741359 0.13085312
#[49] 0.13361390 0.13479076 0.13348272 0.13043844 0.13008776 0.13035932 0.12998105 0.13140665 0.13117472 0.12952669 0.13143115 0.13291106
#[61] 0.13272691 0.13297395 0.13299020 0.13217203 0.13298200 0.13275076 0.13353919 0.13256285 0.13346110 0.13316166 0.13284188 0.13289462
#[73] 0.13233296 0.13132838 0.13160375 0.13095548 0.13014803 0.13014475 0.12966564 0.13011536 0.13000045 0.12975505 0.12974519 0.12868537
#[85] 0.12879276 0.13161376

ggplot(results, aes(x = results$Date, y = results$CFR_Calculation)) + 
  scale_x_date(breaks = seq.Date(from = as.Date("2020-03-07"), 
                                 to = as.Date("2020-05-31"), by = 3)) +
  geom_point() + 
  labs(x = "Date (In 4 Day Interval)", y = "CFR Calculation") + 

check that results$Date is really a Date class object ?

(structure(1L, .Label = c("2020-03-07", "2020-03-08", "2020-03-09", 
                         "2020-03-10", "2020-03-11", "2020-03-12", "2020-03-13", "2020-03-14", 
                         "2020-03-15", "2020-03-16"), class = "factor") -> example_date)

library(lubridate)
(better_date <- ymd(example_date))

class(example_date)
class(better_date)

therefore before you use ggplot() do

results$Date <- ymd(results$Date)

where it says example_date and better_date should I be inserting 2020-03-07 & 2020-05-31 respectively. Sorry i am having trouble understanding your script. As-well, I assume all dates need to be in that column?

Unfortunately I am actually still getting this error, I also tried changing the date format with this, but as-well did not work..

results %>%
  mutate(date=as.Date(Date, format = "d.%m.%Y"))

you could have run my code and observed and understood what it was doing, its not intended for you to put arbitrary dates instead...

the part that directly relates to you is at the bottom where I recommend how to transfor results$Date. Did you try that ?

When checking the data type of the column I find this

 [1] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[13] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[25] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[37] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[49] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[61] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[73] "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer" "integer"
[85] "integer" "integer"

before or after you use the ymd() function as per my recommendation ?

This worked exactly as I had envisioned. Thannks!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

I would use lubridate functions.
what format are your dates?
you can do

dput(head(results$Date))

to share with us a sample , in a reproducible way.

I had the date's manually entered in. Do I need to change this to a date class? If so, how?

but the problem is with your data not the calculations.
I'm not asking you to post a csv, im asking you to post the text result of the dput command here. and it would consist of a set of dates presumably in the range of dates you outlined already. I don't see how unassociated dates could risk anything. if you pass n=1 parameter then it would only share the single first 'date'.

dput(head(results$Date,n=1))

at least try it and see

I cannot link the dataset that I am working with cannot post a .csv here .. however I posted the calculations I am working with at the top.

Unfortunately this was not successful, although maybe the dates are currently strings and they need their variable type changed? Attached is the console output

> dput(head(results$Date,n=1))
structure(1L, .Label = c("2020-03-07", "2020-03-08", "2020-03-09", 
"2020-03-10", "2020-03-11", "2020-03-12", "2020-03-13", "2020-03-14", 
"2020-03-15", "2020-03-16", "2020-03-17", "2020-03-18", "2020-03-19", 
"2020-03-20", "2020-03-21", "2020-03-22", "2020-03-23", "2020-03-24", 
"2020-03-25", "2020-03-26", "2020-03-27", "2020-03-28", "2020-03-29", 
"2020-03-30", "2020-03-31", "2020-04-01", "2020-04-02", "2020-04-03", 
"2020-04-04", "2020-04-05", "2020-04-06", "2020-04-07", "2020-04-08", 
"2020-04-09", "2020-04-10", "2020-04-11", "2020-04-12", "2020-04-13", 
"2020-04-14", "2020-04-15", "2020-04-16", "2020-04-17", "2020-04-18", 
"2020-04-19", "2020-04-20", "2020-04-21", "2020-04-22", "2020-04-23", 
"2020-04-24", "2020-04-25", "2020-04-26", "2020-04-27", "2020-04-28", 
"2020-04-29", "2020-04-30", "2020-05-01", "2020-05-02", "2020-05-03", 
"2020-05-04", "2020-05-05", "2020-05-06", "2020-05-07", "2020-05-08", 
"2020-05-09", "2020-05-10", "2020-05-11", "2020-05-12", "2020-05-13", 
"2020-05-14", "2020-05-15", "2020-05-16", "2020-05-17", "2020-05-18", 
"2020-05-19", "2020-05-20", "2020-05-21", "2020-05-22", "2020-05-23", 
"2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", 
"2020-05-29", "2020-05-30", "2020-05-31"), class = "factor")
> ggplot(results, aes(x = results$Date, y = results$CFR_Calculation)) + 
+   scale_x_date(breaks = seq.Date(from = as.Date("2020-03-07"), 
+                                  to = as.Date("2020-05-31"), by = 3)) +
+   geom_point() + 
+   labs(x = "Date (In 4 Day Interval)", y = "CFR Calculation") + 
+   ggtitle("Case Fatality Rate in Canada") 

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