Home > Mobile >  ggplot Duration object
ggplot Duration object

Time:10-08


I try to plot with ggplotly this kind of DF :

|TIME | DURATION |
|-------------- | --------------|
|02/06/2021 17:36:56 | 02:52:52 |
|02/06/2021 22:07:57 | 11:19:06 |

|03/06/2021 10:34:55 | 07:07:08 |
|03/06/2021 18:22:53 | 02:16:28 |
|03/06/2021 21:23:20 | 04:37:25 |
|04/06/2021 08:48:51 | 03:10:13 |
|04/06/2021 12:53:36 | 00:09:52 |
...

I change my column duration in "period format", with the library lubridate.
Here is my code :

'DF[,1] <- as.POSIXct(strptime(DF[,1],"%d/%m/%Y %H:%M:%S"), tz="GMT")

DF[,"Duration"] <- hms(DF[,"Duration"])

A=ggplot(DF, aes(Time, Duration)) geom_point()'

R return : "Warning message: Removed 112 rows containing missing values (geom_point). "

It seems that it doesn't understand this kind of format... Nevertheless function plot works : 'plot(DF$Time, DF$Duration)' so I don't understand why ggplot doesn't work.

Any idea?

Thanks a lot.

Julie

CodePudding user response:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

df <- tribble(
  ~TIME, ~DURATION,
  "02/06/2021 17:36:56", "02:52:52",
  "02/06/2021 22:07:57", "11:19:06",
  "03/06/2021 10:34:55", "70:07:08"
)
df
#> # A tibble: 3 x 2
#>   TIME                DURATION
#>   <chr>               <chr>   
#> 1 02/06/2021 17:36:56 02:52:52
#> 2 02/06/2021 22:07:57 11:19:06
#> 3 03/06/2021 10:34:55 70:07:08

df <-
  df %>%
  mutate(
    TIME = TIME %>% parse_date_time("%d/%m/%Y %H:%M:%S"),
    DURATION = DURATION %>% hms()
  )
df
#> # A tibble: 3 x 2
#>   TIME                DURATION  
#>   <dttm>              <Period>  
#> 1 2021-06-02 17:36:56 2H 52M 52S
#> 2 2021-06-02 22:07:57 11H 19M 6S
#> 3 2021-06-03 10:34:55 70H 7M 8S

df %>%
  ggplot(aes(TIME, DURATION))  
  geom_point()  
  scale_x_time()  
  scale_y_time()

Created on 2021-10-08 by the reprex package (v2.0.1)

  • Related