rethomics / ggetho

Visualise high throughput behavioural data in R, based on ggplot2
http://rethomics.github.io
8 stars 3 forks source link

time_wrap creates error "Incompatible classes: <numeric> %% <Period>" #48

Open EamonnMallon opened 1 year ago

EamonnMallon commented 1 year ago

Hi,

I'm having this problem analysing my own data. I managed to recreate it with the example given in https://rethomics.github.io/ggetho.html

library(damr)
library(dplyr)
library(lubridate)
library(ggetho)
library(ggplot2)
library(sleepr)
library(zeitgebr)

metadata_test <- data.table(id=sprintf("toy_experiment|%02d" , 1:40), region_id=1:40,
                       condition=c("A","B"),
                       sex=c("M","M", "F", "F"))
dt <- toy_activity_data(metadata_test, seed=107)
ggetho(dt, aes(x=t, y=moving, colour=sex)) + stat_pop_etho()#this command works fine
ggetho(dt, aes(x=t, y=moving), time_wrap = hours(24)) + stat_pop_etho()

I then get this error

Error: Incompatible classes: <numeric> %% <Period>

As this is happening with the toy example, I assume it is something to do with other packages perhaps lubridate?

qgeissmann commented 1 year ago

Hi @EamonnMallon, Thanks for reporting. I will look into that. I am in a bit of a busy time ATM, but should have some time next week. Don't hesitate to update or remind me

EamonnMallon commented 1 year ago

For info

dt[, phase := ifelse(t %% hours(24) < hours(12), "L", "D")]

creates the same error

Error: Incompatible classes: <numeric> %% <Period> but

dt[, phase := ifelse(as.period(t) %% hours(24) < hours(12), "L", "D")]

works.

So I guess it is sometime to do with t being a numeric but timewrap needing period data?

EamonnMallon commented 1 year ago

dt_curated$t<-as.period(dt_curated$t) # doesn't work

ggetho(dt_curated, aes(y=asleep, colour=as.factor(sugar)), time_wrap = hours(24)) + stat_pop_etho() + stat_ld_annotations() + facet_grid(as.factor(agar) ~ .) + scale_y_continuous(name= "Fraction of time sleeping",labels = scales::percent)

Just trying to change it to period before running the command doesn't work

EamonnMallon commented 1 year ago
ggetho(dt_curated, aes(y=asleep, colour=sleepDeprived), time_wrap = mins(1440)) +
stat_pop_etho() +
stat_ld_annotations() +
scale_y_continuous(name= "Fraction of time sleeping", limits = c(0,1), labels = scales::percent)

This worked. That is when I expressed the timewrap in minutes. So my issue is completely resolved. Not sure if it was due to my data or a more general problem.

epetersen-sci commented 9 months ago

I had this issue as well. It comes from a conflict with the lubridate package. I don't load that package anymore and it seems to fix the issue. I'm not sure where lubridate is being used in the rest of the code, but I haven't run into any issues as of yet

EamonnMallon commented 9 months ago

I'll give that a try as well. Thanks @epetersen-sci