R 如何使用scale_x_time将x轴设置为某个小时,直到第二天的同一小时?

R 如何使用scale_x_time将x轴设置为某个小时,直到第二天的同一小时?,r,ggplot2,time-series,scale,axis,R,Ggplot2,Time Series,Scale,Axis,我试图在ggplot2中可视化动物测量的时间序列数据 在y轴中,我有不同数据集的天数组,在x轴中,我有白天时间(小时:分钟:秒格式),并将颜色设置为与测定是在白天还是在黑暗中记录相关的系数 我的问题是,我需要将测量起点设置为每天早上7:45,终点设置为第二天早上7:45 我成功地将数据按for循环(重复到我们做实验的时候)分成几天: 我试图添加此scale\u x\u time(),但随后这些点消失了: scale_x_time( labels = "%H:%M",

我试图在ggplot2中可视化动物测量的时间序列数据

在y轴中,我有不同数据集的天数组,在x轴中,我有白天时间(小时:分钟:秒格式),并将颜色设置为与测定是在白天还是在黑暗中记录相关的系数

我的问题是,我需要将测量起点设置为每天早上7:45,终点设置为第二天早上7:45

我成功地将数据按for循环(重复到我们做实验的时候)分成几天:

我试图添加此
scale\u x\u time()
,但随后这些点消失了:

scale_x_time(
    labels = "%H:%M", 
    breaks = "2 hours", 
    limits = c(as.hms('07:45:00'),
               as.hms('07:44:00')),
    timezone = Sys.timezone(),
    expand = c(0,0)
  ) + 

任何修改绘图的想法都是受欢迎的。

我只是想告诉大家,我通过计算差异获得了正确的绘图

如果值和午夜的差值小于我的截止值和午夜的差值,则表示它在起点和午夜之间的时间范围内,并且我保持该值不变

如果差值更大,这意味着它实际上是午夜和第二天截止时间之间的时间范围,我要做的是将值和午夜的值相加。 之后,我在ggplot2命令中更改标签

df2$crono <- 0
for(i in 1:nrow(df2)) {
  if((as_hms('24:00:00') - as_hms(df2$Take_Time[i])) <= (as_hms('24:00:00') -
                                                            as_hms('07:45:00'))) {
    df2$crono[i] <- ((as_hms(df2$Take_Time[i])))
  } else df2$crono[i] <- ((as_hms('24:00:00') + as_hms(df2$Take_Time[i])))
}

ggplot(data = df2, aes(x = crono,y= as.factor(Day))) +
  geom_point(aes(col = as.factor(Light_dark)),alpha= 0.6, size = 2)+
  labs(subtitle="Feeding system.",y="Day",x="Time (h:m:s)",title="Distribution of the data", 
       color = "Lightness intervals")+
  scale_color_manual(values = c('aquamarine3', 'chocolate')) +  
  scale_x_continuous( limits = c( 25000, 115000),
                      breaks= c(28800,43200,57600,72000,86400,100800,115200),
                      labels=c("08:00","12:00","16:00","20:00","24:00","04:00","08:00"))+ 
theme_minimal() + theme(axis.text.x = element_text(angle = 45,hjust = 1))

df2$crono请您共享足够的数据,使您的问题重现。请使用
dput(df)
将数据转换为易于导入的格式谢谢。我最终将ggplot图分成两个图:一个是从早上7:45到午夜的x轴图,另一个是从午夜到第二天早上7:45的x轴图。
df2$Day <- as.factor(df2$Day)
df2 <- df2 %>%
  group_by(Day, Light_dark)
ggplot(data = df2, aes(x = Take_Time,y= as.factor(Day))) +
  geom_point(aes(col = as.factor(Light_dark)),alpha= 0.6, size = 2)+
  labs(subtitle="Feeding system.",y="Day",x="Time (h:m:s)",title="Distribution of the data", color = "Lightness intervals")+
  scale_color_manual(values = c('aquamarine3', 'chocolate')) +  
  theme_minimal() + theme(axis.text.x = element_text(angle = 45,hjust = 1))
scale_x_time(
    labels = "%H:%M", 
    breaks = "2 hours", 
    limits = c(as.hms('07:45:00'),
               as.hms('07:44:00')),
    timezone = Sys.timezone(),
    expand = c(0,0)
  ) + 
df2$crono <- 0
for(i in 1:nrow(df2)) {
  if((as_hms('24:00:00') - as_hms(df2$Take_Time[i])) <= (as_hms('24:00:00') -
                                                            as_hms('07:45:00'))) {
    df2$crono[i] <- ((as_hms(df2$Take_Time[i])))
  } else df2$crono[i] <- ((as_hms('24:00:00') + as_hms(df2$Take_Time[i])))
}

ggplot(data = df2, aes(x = crono,y= as.factor(Day))) +
  geom_point(aes(col = as.factor(Light_dark)),alpha= 0.6, size = 2)+
  labs(subtitle="Feeding system.",y="Day",x="Time (h:m:s)",title="Distribution of the data", 
       color = "Lightness intervals")+
  scale_color_manual(values = c('aquamarine3', 'chocolate')) +  
  scale_x_continuous( limits = c( 25000, 115000),
                      breaks= c(28800,43200,57600,72000,86400,100800,115200),
                      labels=c("08:00","12:00","16:00","20:00","24:00","04:00","08:00"))+ 
theme_minimal() + theme(axis.text.x = element_text(angle = 45,hjust = 1))