Home > Net >  Using breaks in Scale_x_datetime
Using breaks in Scale_x_datetime

Time:05-30

I am trying to plot a graph with limits from 2022-05-29 07:00:00 to 2022-05-29 07:00:00 PM on the x axis. Within these limits I would like 1 hour breaks. I am using scale_x_datetime so that I can graph as continuous variable instead of discrete. I created a vector with all breaks called axisorder.

c("2022-05-29 07:00:00", "2022-05-29 08:00:00", "2022-05-29 09:00:00", 
"2022-05-29 10:00:00", "2022-05-29 11:00:00", "2022-05-29 12:00:00", 
"2022-05-29 01:00:00", "2022-05-29 02:00:00", "2022-05-29 03:00:00", 
"2022-05-29 04:00:00", "2022-05-29 05:00:00", "2022-05-29 06:00:00", 
"2022-05-29 07:00:00", "2022-05-29 08:00:00")

here is my data

structure(list(Id = c("user_6", "user_6", "user_6", "user_6", 
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", 
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", 
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6"), 
    ActivityHour = structure(c(1653825600, 1653786000, 1653789600, 
    1653793200, 1653796800, 1653800400, 1653804000, 1653807600, 
    1653811200, 1653814800, 1653818400, 1653822000, 1653825600, 
    1653786000, 1653789600, 1653793200, 1653796800, 1653800400, 
    1653804000, 1653807600, 1653811200, 1653814800, 1653818400, 
    1653822000), class = c("POSIXct", "POSIXt"), tzone = ""), 
    TotalIntensity = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 28L, 13L, 
    13L, 143L, 20L, 11L, 19L, 54L, 9L, 19L, 24L, 19L, 9L, 0L, 
    0L, 0L, 0L), AverageIntensity = c(0, 0, 0, 0, 0, 0, 0, 0.466667, 
    0.216667, 0.216667, 2.383333, 0.333333, 0.183333, 0.316667, 
    0.9, 0.15, 0.316667, 0.4, 0.316667, 0.15, 0, 0, 0, 0)), row.names = c(NA, 
-24L), class = "data.frame")

and last but not least my graph code

ggplot(data=df2, aes(x = ActivityHour, y = AverageIntensity))  
  geom_point()  
  geom_line()  
  theme(axis.text.x = element_text(angle = 45))   
  scale_x_datetime(breaks = "axisorder"), limits = c(as.POSIXct("2022-05-29 07:00:00"),as.POSIXct("2022-05-29 07:00:00"))  
  ggtitle("Average Intensity user_6",
          subtitle = "4-12-2016")

I just don't understand how to set up scale_x_datetime to have those limits and breaks? My x axis data is already formatted in (posixct.)

Any help would be appreciated!

UPDATE -- Here is where I am at..

I have converted my df2$ActivityHour to POSIXct

1653800400, 1653804000, 1653807600, 1653811200, 1653814800, 1653818400, 
1653822000, 1653825600, 1653786000, 1653789600, 1653793200, 1653796800, 
1653800400, 1653804000, 1653807600, 1653811200, 1653814800, 1653818400, 
1653822000), class = c("POSIXct", "POSIXt"), tzone = "")

I then updated date_breaks = "hour" and changed my limits limits = c(as.POSIXct("2022-05-29 07:00:00"), as.POSIXct("....))

my new plot data looks like this

  ggplot(data=df2, aes(x = ActivityHour, y = AverageIntensity))  
  geom_point()  
  geom_line()  
  theme(axis.text.x = element_text(angle = 45))   
  scale_x_datetime(date_breaks = "hour", limits = c(as.POSIXct("2022-05-29 07:00:00"),as.POSIXct("2022-05-29 19:00:00")))  
  ggtitle("Average Intensity user_6",
          subtitle = "4-12-2016")

my plot comes out weird, as if df2$AverageIntensity is not plotting values correctly with $ActivityHour values. For instance, 13:00:00 no longer correlates to averageintensity of .31 INSTEAD there is no data. I understand this is because df2$ActivityHour values after 12:00:00 should be 13:00:00, 14:00:00, 15:00:00, etc. not back to 01:00:00. so I am looking for the quickest way to fix this?

UPDATE2 -- I fixed this with replace function

df2$ActivityHour <- replace(df2$ActivityHour,1,"2022-05-29 00:00:00")

and then replaced by position for each one.

Syntax: replace(list , position , replacement_value)

CodePudding user response:

You have a couple of issues.

Firstly, your axisorder is in character format rather than the required POSIXct. Secondly, you have put "axisorder" in quotation marks, so ggplot thinks you want a single break at a location called "axisorder". You don't actually need axisorder anyway, since, as Stefan points out, you can use date_breaks = "hour"

Thirdly, and most importantly, you need to use 24-hour time formatting, so your second limit should be "2022-05-29 19:00:00". The same is also true for all the times in your data frame. The times after midday require 12 hours added on to them (and the initial value at midnight needs 12 hours removed):

df2$ActivityHour[14:24] <- df2$ActivityHour[14:24]   12 * 3600
df2$ActivityHour[1] <- df2$ActivityHour[1] - 12 * 3600

ggplot(data=df2, aes(x = ActivityHour, y = AverageIntensity))  
  geom_point()  
  geom_line()  
  theme(axis.text.x = element_text(angle = 45, hjust = 1))   
  scale_x_datetime(date_breaks = "hour",
                   limits = c(as.POSIXct("2022-05-29 07:00:00"),
                              as.POSIXct("2022-05-29 19:00:00")))  
  ggtitle("Average Intensity user_6", subtitle = "4-12-2016")

enter image description here

  • Related