Issues with column format and plotting using ggplot2

I'm using ggplot2 to graph averaged data (y) against the time of year (x). The issues is, when I try to format the data using the scale_x_datetime function it always reads: Error: Invalid input: time_trans works with objects of class POSIXct only

I reformatted the column I am plotting to be POSIXct but when I plot it again I just get the same error message.

Here is the code:
exports_15E$Date_time <- as.POSIXct(paste(as.Date(as.character(exports_15E$Date_M),"%Y%m%d"), exports_15E$Time_M, sep=" "),format = "%Y-%m-%d %H:%M", tz="Asia/Bangkok")
exports_15E$Sv_Ln = 10^(exports_15E$Sv_mean/10)
desiredGroupingUnit15E = cut(exports_15E$Date_time, breaks = "24 hour") #You can use substr for that also
aggregate(exports_15E$Sv_Ln, by = list(desiredGroupingUnit15E), FUN = mean)
daily_avg_15E<- data.frame(aggregate(exports_15E$Sv_Ln, by = list(desiredGroupingUnit15E), FUN = mean))
daily_avg_15E$Sv_log = 10*log10(daily_avg_15E$x)

daily_avg_15E$Date <- as.POSIXct(daily_avg_15E$Group.1, format = "%Y-%m-%d", tz="Asia/Bangkok")

ggplot(daily_avg_15E, aes(x = Date, y = Sv_log)) + geom_point() + geom_smooth(method = loess, se = TRUE, fullrange= TRUE) +
labs(y = "Sv mean 24 Hour Avg (dB)", x = "Date/Time") + ggtitle("Dai 15E Dec-Jan Sv mean Daily") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = NA), legend.position = ("none")) +
scale_x_datetime(name = "Date/Time", breaks = c("1 week")) +
annotate("segment", x= 21, xend= 21, y=-53.0, yend=-70) +
annotate("segment", x= 27, xend= 27, y=-53.0, yend=-70) +
annotate("segment", x= 52, xend= 52, y=-53.0, yend=-70) +
annotate("segment", x= 58, xend= 58, y=-53.0, yend=-70)

I think it may have something to do with how I averaged the data, might try another averaging method.

Can you post the output from

dput(head(daily_avg_15E, 10))

Please place a line with three back ticks just before and just after the output, like this
```
Your output here
```

Here you go:
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")

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"))

Created on 2021-06-16 by the reprex package (v2.0.0)

Yes, there are some extra columns in there from me trying different solutions. And my organisation skills need some work.

I am aware of the difference between scale_x_date and scale_x_datetime. I have 2 columns that are formatted with either. Issue is, whenever I plug in the "Date" formatted column, the scale_x_date function also tells me that I have to use data formatted as "Date". What is frustrating is that said column is formatted as class "Date". This problem also occurs when I use the POSIXct alternative.

I can plot the data when I use the "Group.1" column, which was generated alongside the "x" column when I did the averaging. However, this results in the following plot:

I have yet to be able to edit the x-axis to make it readable as scale_x_discrete and scale_x_continuous don't work either.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.