Home > Blockchain >  How do you set x axis limits for times of type hms?
How do you set x axis limits for times of type hms?

Time:03-18

I made a histogram like plot with ggplot2 using geombar. The x axis is of type hms.

library(dplyr)
library(ggplot2)

ggplot(df, aes(x=end_time, y=n))  
  geom_bar(stat="identity")    theme(axis.text.x=element_text(angle=90,vjust=0.5)) 

enter image description here

I've been trying to set the limits of the x axis using scale_x_continuous, but I haven't been able to get it to work. Is there a correct way to do this?

DATA

df<-structure(list(end_time = structure(c(13500, 32400, 33300, 33300, 
33300, 34200, 35100, 35100, 35100, 36000, 36000, 36900, 36900, 
37800, 37800, 38700, 38700, 39600, 39600, 40500, 40500, 40500, 
41400, 41400, 41400, 42300, 42300, 43200, 43200, 43200, 44100, 
44100, 45000, 45000, 45000, 45000, 45000, 45000, 45900, 45900, 
45900, 45900, 46800, 46800, 46800, 46800, 46800, 46800, 46800, 
46800, 47700, 47700, 47700, 47700, 47700, 48600, 48600, 48600, 
49500, 49500, 49500, 49500, 49500, 49500, 49500, 49500, 50400, 
50400, 50400, 50400, 50400, 50400, 50400, 50400, 50400, 51300, 
51300, 51300, 51300, 52200, 52200, 52200, 52200, 52200, 53100, 
53100, 53100, 53100, 53100, 53100, 53100, 53100, 54000, 54000, 
54000, 54000, 54000, 54000, 54000, 54000, 54900, 54900, 54900, 
54900, 54900, 54900, 54900, 54900, 54900, 54900, 54900, 54900, 
54900, 55800, 55800, 55800, 55800, 55800, 55800, 55800, 55800, 
55800, 55800, 56700, 56700, 56700, 56700, 56700, 56700, 56700, 
56700, 56700, 56700, 56700, 56700, 56700, 57600, 57600, 57600, 
57600, 57600, 57600, 57600, 57600, 57600, 57600, 57600, 57600, 
57600, 58500, 58500, 58500, 58500, 58500, 58500, 58500, 58500, 
58500, 58500, 58500, 58500, 59400, 59400, 59400, 59400, 59400, 
59400, 59400, 59400, 59400, 59400, 59400, 59400, 59400, 59400, 
60300, 60300, 60300, 60300, 60300, 60300, 60300, 60300, 60300, 
60300, 60300, 60300, 60300, 61200, 61200, 61200, 61200, 61200, 
61200, 61200, 61200, 61200, 61200, 61200, 61200, 61200, 61200, 
62100, 62100, 62100, 62100, 62100, 62100, 62100, 62100, 62100, 
62100, 62100, 62100, 62100, 62100, 63000, 63000, 63000, 63000, 
63000, 63000, 63000, 63000, 63000, 63000, 63000, 63000, 63000, 
63900, 63900, 63900, 63900, 63900, 63900, 63900, 63900, 63900, 
63900, 63900, 63900, 64800, 64800, 64800, 64800, 64800, 64800, 
64800, 64800, 64800, 64800, 65700, 65700, 65700, 65700, 65700, 
65700, 65700, 65700, 65700, 65700, 65700, 66600, 66600, 66600, 
66600, 66600, 66600, 66600, 67500, 67500, 67500, 67500, 67500, 
67500, 67500, 68400, 68400, 68400, 68400, 68400, 68400, 69300, 
69300, 69300, 69300, 69300, 70200, 70200, 71100, 72000, 72000, 
72000, 73800, 74700, 76500, 79200, 83700), class = c("hms", "difftime"
), units = "secs"), n = c(1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
1L, 2L, 1L, 3L, 1L, 4L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
3L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 6L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 
2L, 2L, 4L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 4L, 1L, 2L, 1L, 1L, 5L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 2L, 1L, 4L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 5L, 1L, 1L, 3L, 3L, 4L, 1L, 1L, 2L, 3L, 
1L, 2L, 2L, 4L, 1L, 1L, 1L, 3L, 1L, 2L, 3L, 7L, 1L, 1L, 3L, 2L, 
1L, 4L, 3L, 1L, 1L, 2L, 6L, 4L, 6L, 3L, 4L, 6L, 1L, 2L, 2L, 4L, 
2L, 2L, 3L, 2L, 1L, 3L, 3L, 5L, 1L, 3L, 2L, 3L, 1L, 8L, 2L, 5L, 
4L, 2L, 1L, 2L, 10L, 4L, 2L, 6L, 1L, 1L, 2L, 5L, 5L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 6L, 5L, 3L, 4L, 5L, 6L, 
3L, 6L, 4L, 8L, 6L, 4L, 1L, 2L, 3L, 5L, 3L, 6L, 5L, 6L, 4L, 4L, 
4L, 4L, 5L, 7L, 1L, 4L, 1L, 3L, 5L, 6L, 3L, 3L, 6L, 6L, 7L, 5L, 
2L, 7L, 7L, 3L, 4L, 4L, 1L, 2L, 7L, 2L, 5L, 4L, 5L, 1L, 1L, 4L, 
6L, 8L, 7L, 7L, 7L, 1L, 4L, 5L, 7L, 9L, 3L, 2L, 3L, 5L, 1L, 6L, 
6L, 6L, 6L, 8L, 3L, 6L, 9L, 2L, 4L, 6L, 3L, 6L, 4L, 5L, 4L, 3L, 
4L, 4L, 1L, 2L, 4L, 4L, 4L, 2L, 1L, 3L, 1L, 2L, 2L, 1L, 5L, 5L, 
3L, 4L, 2L, 2L, 1L, 8L, 1L, 1L, 2L, 5L, 1L, 1L, 2L, 4L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L)), row.names = c(NA, -298L), class = c("tbl_df", "tbl", 
"data.frame"))

CodePudding user response:

One way to do this is to define end_time as as.POSIXct

Sample code:

library(dplyr)
library(ggplot2)

df%>%
  mutate(end_time = as.POSIXct(end_time), 
         end_time= if_else(end_time < as.POSIXct('1970-01-01 04:00:00', 'UTC'), end_time   86400, end_time)) %>%

    ggplot(aes(x=end_time, y=n))  
      geom_bar(stat="identity")    
      labs(x="End time", y="Count") 
      theme_bw() 
      theme(axis.text.x = element_text(hjust = 1,family="Times", face="bold", size=12, color="black"), 
            axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
            axis.text.y = element_text(family="Times", face="bold", size=12, color="black"),
            axis.title.y = element_text(family="Times", face="bold", size=16, color="black"),
            strip.text = element_text(size=10, face="bold"),
            plot.title = element_text(size=20, face="bold"),
            legend.title = element_blank(),
            legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
            legend.position="none") 
      scale_y_continuous(expand = expansion(mult = c(0, .1)))   
      scale_x_datetime(date_labels = '%H:%M', 
                       limits = c(as.POSIXct('1970-01-01 04:00:00', tz = 'UTC'), 
                                  as.POSIXct('1970-01-02 03:50:00', tz = 'UTC')), 
                       breaks = '2 hour')

Plot:

enter image description here

  • Related