I'm struggling to replicate your issues exactly with the code and sample data provided. But I think I can help anyway with the example below.
Sample data:
exports_15E <- structure(list(Group.1 = structure(1:10, .Label = c("2020-12-01 18:00:00",
"2020-12-02 18:00:00", "2020-12-03 18:00:00", "2020-12-04 18:00:00",
"2020-12-05 18:00:00", "2020-12-06 18:00:00", "2020-12-07 18:00:00",
"2020-12-08 18:00:00", "2020-12-09 18:00:00", "2020-12-10 18:00:00",
"2020-12-11 18:00:00", "2020-12-12 18:00:00", "2020-12-13 18:00:00",
"2020-12-14 18:00:00", "2020-12-15 18:00:00", "2020-12-16 18:00:00",
"2020-12-17 18:00:00", "2020-12-18 18:00:00", "2020-12-19 18:00:00",
"2020-12-20 18:00:00", "2020-12-21 18:00:00", "2020-12-22 18:00:00",
"2020-12-23 18:00:00", "2020-12-24 18:00:00", "2020-12-25 18:00:00",
"2020-12-26 18:00:00", "2020-12-27 18:00:00", "2020-12-28 18:00:00",
"2020-12-29 18:00:00", "2020-12-30 18:00:00", "2020-12-31 18:00:00",
"2021-01-01 18:00:00", "2021-01-02 18:00:00", "2021-01-03 18:00:00",
"2021-01-04 18:00:00", "2021-01-05 18:00:00", "2021-01-06 18:00:00",
"2021-01-07 18:00:00", "2021-01-08 18:00:00", "2021-01-09 18:00:00",
"2021-01-10 18:00:00", "2021-01-11 18:00:00", "2021-01-12 18:00:00",
"2021-01-13 18:00:00", "2021-01-14 18:00:00", "2021-01-15 18:00:00",
"2021-01-16 18:00:00", "2021-01-17 18:00:00", "2021-01-18 18:00:00",
"2021-01-19 18:00:00", "2021-01-20 18:00:00", "2021-01-21 18:00:00",
"2021-01-22 18:00:00", "2021-01-23 18:00:00", "2021-01-24 18:00:00",
"2021-01-25 18:00:00", "2021-01-26 18:00:00", "2021-01-27 18:00:00",
"2021-01-28 18:00:00"), class = "factor"), x = c(4.32907002470739e-07,
4.2477833808932e-07, 4.14693930274232e-07, 3.39807033821434e-07,
2.42350588302866e-07, 3.18842870226681e-07, 2.94186594995173e-07,
2.3120408561686e-07, 2.54900508695861e-07, 2.40807691174125e-07
), Sv_log = c(-63.6360538921617, -63.7183763855593, -63.8227232120145,
-64.6876763571353, -66.155559216958, -64.9642329000127, -65.3137712036533,
-66.3600449574814, -65.9362929782385, -66.1832964623008), Date = structure(c(1606755600,
1606842000, 1606928400, 1607014800, 1607101200, 1607187600, 1607274000,
1607360400, 1607446800, 1607533200), class = c("POSIXct", "POSIXt"
), tzone = "Asia/Bangkok"), Time = structure(c(18597, 18598,
18599, 18600, 18601, 18602, 18603, 18604, 18605, 18606), class = "Date"),
POSIXct = structure(c(1606755600, 1606842000, 1606928400,
1607014800, 1607101200, 1607187600, 1607274000, 1607360400,
1607446800, 1607533200), class = c("POSIXct", "POSIXt"), tzone = "Asia/Bangkok")), row.names = c(NA,
10L), class = "data.frame")
Note in your data frame, the Date
and POSIXct
variabless is date and time (dttm), and Time
, confusingly, is a date variable. POSIXct is a dttm variable, storing date and time (more details if you're curious).
library(dplyr)
exports_15E <- exports_15E %>%
tibble
exports_15E
#> # A tibble: 10 x 6
#> Group.1 x Sv_log Date Time POSIXct
#> <fct> <dbl> <dbl> <dttm> <date> <dttm>
#> 1 2020-12-0… 4.33e-7 -63.6 2020-12-01 00:00:00 2020-12-01 2020-12-01 00:00:00
#> 2 2020-12-0… 4.25e-7 -63.7 2020-12-02 00:00:00 2020-12-02 2020-12-02 00:00:00
#> 3 2020-12-0… 4.15e-7 -63.8 2020-12-03 00:00:00 2020-12-03 2020-12-03 00:00:00
#> 4 2020-12-0… 3.40e-7 -64.7 2020-12-04 00:00:00 2020-12-04 2020-12-04 00:00:00
#> 5 2020-12-0… 2.42e-7 -66.2 2020-12-05 00:00:00 2020-12-05 2020-12-05 00:00:00
#> 6 2020-12-0… 3.19e-7 -65.0 2020-12-06 00:00:00 2020-12-06 2020-12-06 00:00:00
#> 7 2020-12-0… 2.94e-7 -65.3 2020-12-07 00:00:00 2020-12-07 2020-12-07 00:00:00
#> 8 2020-12-0… 2.31e-7 -66.4 2020-12-08 00:00:00 2020-12-08 2020-12-08 00:00:00
#> 9 2020-12-0… 2.55e-7 -65.9 2020-12-09 00:00:00 2020-12-09 2020-12-09 00:00:00
#> 10 2020-12-1… 2.41e-7 -66.2 2020-12-10 00:00:00 2020-12-10 2020-12-10 00:00:00
The scale_x_datetime
requires a a POSIXct only.
I run into this error myself most commonly when I try to use scale_x_datetime
on a date variable.
I'd resolve this by either passing one of your date-and-time variables, or use `scale_x_date. Examples below.
library(ggplot2)
# In your example, `Time`, the x-axis var, is a DATE variable. Reproducing the error.
ggplot(exports_15E, aes(x = Time, y = Sv_log)) +
geom_point() +
scale_x_datetime(name = "Date/Time", breaks = c("1 week"))
#> Error: Invalid input: time_trans works with objects of class POSIXct only
# In your example, `Date`, the x-axis var, is a date and time variable.
ggplot(exports_15E, aes(x = Date, y = Sv_log)) +
geom_point() +
scale_x_datetime(name = "Date/Time", breaks = c("1 week"))
![](https://forum.posit.co/uploads/default/original/3X/b/0/b05d632630af5e5900832bfe97db0578b49a89d0.png)
Created on 2021-06-16 by the reprex package (v2.0.0)