Get rid of seconds in ggplot x axis

I have imported a 24 h time series from Excel with a time stamp every 15 m. I have converted the time to an 'hms' object. When I use this code:

# Plot
library(ggplot2)
plot7<- ggplot (data = day7)+
	geom_line (aes(x =time, y = ICP), color="darkred", linewidth= 1) +
	geom_line (aes(x =time, y = MABP), color="blue", linewidth= 1) +
	geom_line (aes(x =time, y = SaO2), color= "red", linewidth= 1)+
	ylim (0, 110)+
	labs (x = "Time (h)", y="mmHg")
plot7

I get the plot with HH.MM.SS format. I would like to have only 17:15 format and not 17:15:00. I have tried many options without success
Any help would be appreciated.
Thanks

Have you tried adding the layer scale_x_time() or scale_x_time()? Inside those you can define the labels,. The first works on hms objects, the second on POSIXct.
To be honest, I think it's easiest to use the latter. To do this, make sure your column time is a POSIXct class, e.g. by using a lubridate function like mdy_hms(). And if this is all one day, you could use dummy values for the month, day and year. Then add scale_x_datetime(date_labels = "%H:%M") and it will only show the hours and the minutes on your x-axis.

@GeraldineK
Thanks a lot for your reply, I tried different ways without success...
Because time is class "hms" I used this code:

labs (x = "Time (h)", y="mmHg") +
	scale_x_time (breaks="24 hours", labels ="%H:%M")

However I get the error

! `breaks` and `labels` are different lengths

If I use only

	scale_x_time (breaks="24 hours", labels ="%H:%M")

I get the same error...
Thanks

Would something like the second plot below work for you?

library(hms)
library(ggplot2)
DF <- data.frame(Time = seq(0, 60*60*24-900, 900), Val = runif(96, 10,20))
DF$Time <- hms(DF$Time)
head(DF)
#>       Time      Val
#> 1 00:00:00 12.13437
#> 2 00:15:00 14.48042
#> 3 00:30:00 13.43192
#> 4 00:45:00 16.60827
#> 5 01:00:00 12.58694
#> 6 01:15:00 17.59967
BREAKS <- c(0, 6,12,18,24)*3600
LABELS <- format(as.POSIXct(BREAKS, tz = "UTC"), format = "%H:%M")
ggplot(DF, aes(Time, Val)) + geom_line()

ggplot(DF, aes(Time, Val)) + geom_line() + 
  scale_x_time(breaks = BREAKS, labels = LABELS)

Created on 2024-01-27 with reprex v2.0.2

1 Like

Yes, indeed - scale_x_time() will want you to actually supply a vector with the breaks. Which I think is a hassle and hence why I suggested creating a POSIXct object and using scale_x_datetime().

But the solution by @FJCC deals with the breaks = argument by creating a vector BREAKS which solves that. That is an excellent solution.
But keep in mind that that approach requires you to "hard code" the breaks and the labels, so if you have to make these plots for a lot of datasets with a different numbers of breaks and labels (i.e. not necessarily one day / 24 hours, or if you want different labels on different plots), it will require more coding.

Hi,
Thanks a lot for your valuable help.
The imported 'Time' in Excel is already a 'POSIXct' object.

[1] "POSIXct" "POSIXt" 

Yes, I need to generate daily plots regularly
Finally I find out the solution with this code

scale_x_datetime(date_breaks= "2 hours", date_labels = "%H:%M")

It generates this plot

![image|690x431](upload://417UDSfWfhMHGmrdCGyVwXZjld1.png)

Thanks again for your help

This topic was automatically closed 7 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.