Home > Mobile >  Remove date from x axis in 24h histogram in R (ggplot)
Remove date from x axis in 24h histogram in R (ggplot)

Time:04-28

I have stripped the date from my timestamps in a dataset so I can plot the logins by the hour.

MV1$starttime <- format(MV1$earliestlog,format='%H:%M:%S')
MV1$starttime <- as.POSIXct(paste(Sys.Date(), MV1$starttime),  #Adds todays date
                                    format = "%Y-%m-%d %H:%M:%S")

When I plot it with

MV1 %>% 
  ggplot(aes(x = starttime))  
  geom_histogram(binwidth=30, )  
  labs(title = "logins vs daytime between 2018 and 2020",
       subtitle = "Source: , Bin size = 30",
       x = "Time of day",
       y = "logins") 

Histogram I get what I want, except I want to remove todays date from the x axis and only keep hourly time labels.

I tried adding

  scale_x_discrete(breaks=c(as.POSIXct(paste(Sys.Date(), "00:00"))),
                   labels=c("00:00"))

but I get this error:

Error in new_mapped_discrete(): ! mapped_discrete objects can only be created from numeric vectors

I also tried to get rid of the date in my df, but that does not seem to work either / is then not graphable as that is only a character column then. Looks like POSIX needs a date...

MV1$starttime <- format(as.POSIXct(strptime(MV1$starttime, "%Y-%m-%d %H:%M:%S",tz="")), format='%H:%M:%S')
MV1$starttime <- as_date(MV1$starttime)

Any idea how I can just remove the date from the x axis in the graph and maybe even add some more labels at every full hour or so?

TIA!

CodePudding user response:

You can try formatting the labels of the x-axis using scale_x_datetime() and change the default labels by setting date_format to "%H:00" ("%H" = hours, see ?strptime for available format codes). The date breaks can be set with the parameter date_breaks, which expects a string with a duration (e.g. "1 hour", "2 hours").

Here is an example:

library(ggplot2)

# Generating some timestamps
set.seed(1)
df <- data.frame(
    timestamp = sample(
      seq(as.POSIXct("2022-04-26 00:00:00"), 
          as.POSIXct("2022-04-27 00:00:00"), by = "sec"),
                size = 1000, replace = TRUE))

# Histogram with formatted date 
ggplot(df, aes(timestamp))  
  geom_histogram(binwidth = 3600)   
  scale_x_datetime(date_breaks = "1 hour", date_labels = "%H:00", 
                   expand = c(0, 0))

Hope this helps.

  • Related