Home > Enterprise >  How can you set limits and change x axis for time?
How can you set limits and change x axis for time?

Time:09-22

I have this data:

structure(list(hour = structure(c(0, 3600, 7200, 10800, 14400, 
18000, 21600, 25200, 28800, 32400, 36000, 39600, 43200, 46800, 
50400, 54000, 57600, 61200, 64800, 68400, 72000, 75600, 79200, 
82800), class = c("hms", "difftime"), units = "secs"), sum.hour = c(1103, 
1035, 840, 614, 384, 247, 177, 244, 302, 418, 460, 645, 872, 
946, 896, 873, 998, 1093, 1009, 1005, 980, 977, 1093, 1193)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -24L))

And I want to manipulate the X axis in such a way that allows me to drop hour 24 and also re-label the time component to just read as 0 through 23, rather than 00:00:00. I have tried a variety of things from xlim() to scale_x_datetime, but I have had no luck.

ggplot(data = tweetsperhour, aes(x = hour, y = sum.hour))   
  theme_bw()  
  geom_bar(stat = "identity", position = "dodge", color = "#1DA1F2")  
  labs(title = "Number of Tweets Sent during Hours of the Day (GMT-6)",
       subtitle = "18,404 tweets",
       x = "Hour",
       y = "Number of Tweets")

CodePudding user response:

...
scale_x_continuous(breaks = (60*60)*(0:23), labels = 0:23)  
...

enter image description here

CodePudding user response:

You could convert your hour column to integer first:

library(lubridate)
tweetsperhour %>% 
  mutate(hour = hour(hour)) %>% 
  ggplot(aes(x = hour, y = sum.hour))   
  theme_bw()  
  geom_bar(stat = "identity", position = "dodge", color = "#1DA1F2")  
  labs(title = "Number of Tweets Sent during Hours of the Day (GMT-6)",
       subtitle = "18,404 tweets",
       x = "Hour",
       y = "Number of Tweets")  
  scale_x_continuous(breaks = 0:23)

enter image description here

  • Related