Home > Back-end >  Can i set the limit of y axis which contains time data in Rstudio?
Can i set the limit of y axis which contains time data in Rstudio?

Time:01-21

Hi i have a problem that occurs in R.

I can't handle the y - axis, which is hour : minute format.

here is the code sample.

library("data.table")
library("ggplot2") #load library

time <- c("00:05", "06:57","19:00","23:10","00:32","15:00","01:30","03:20") #time data

class <- c("Math", "Eng", "History","Math", "Eng", "History","Math", "Eng") #Course data

day <- c("1/21","1/21","1/21","1/21","1/22","1/22","1/23","1/23") #day data

df <- data.frame(time, class, day) #dataframe

df$time = as.POSIXct(df$time, format = "%H:%M") #make char to time format

pic <- ggplot(df, aes(x = class, y=df$time))   
  scale_y_datetime(date_breaks = "1 hour")   
  facet_grid(. ~ day) #draw picture, break time in 1 hour

pic

if i draw this graph, then Y axis have the year, month and day. i tried to erase the format of year, month and day through as.POSIXct, but didnt work.

also, the limit of time is not from 00:00 to 24:00, its from 1/20 23:00 to 1/22 00:00

i don't know how to figure out the solution of it.

CodePudding user response:

Combining IanCampbell's mention of date_labels= and changing it to labels= using a enter image description here

Notice the space in the formatting string: " %H:%M". This is because of one hard-coded presumption in format.myPOSIX: that the hour is present and has a space before it and a colon after it. I could possibly fix the function to be more robust, but ... perhaps this is sufficient for now.

  •  Tags:  
  • Related