Search code examples
rggplot2axis-labels

R - How to make a plot with two variables on the horizontal axis?


I'm a rookie in R.

This is my date frame:

 I  UserID | hour | min | velocity
   #1    1        0     0      12
   #2    1        0     30     20
   #3    1        1     0      19
   #4    1        1     30     11
   #5    1        2     0      12
   #6    1        2     30     7
   ..   ...     ...    ...   ....
   #10   2        0     0      142
   #11   2        0     30     201
   #12   2        1     0      129
   #13   2        1     30     111
   ..   ...     ...    ...   ....

I put the Userid column as a factor.

My question is how can I make a linear graph using as the horizontal axis the hour and minute column and velocity as the vertical axis?


Solution

  • if you want only time to appear in x-axis, you can create a dummy date "2018-01-01" in this case and exclude it from the plot

    library(lubridate)
    library(scales)
    df$Time=ymd_hm(paste0("2018-01-01"," ",as.character(df$hour),":",as.character(df$min)))
    ggplot(df,aes(x=Time,y=velocity))+geom_line()+scale_x_datetime(labels = date_format("%H:%M", tz = "UTC"))
    

    enter image description here