ggplot 持续时间对象

ggplot Duration object

我尝试用 ggplotly 绘制这种 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

我使用库 lubridate 以“句点格式”更改了我的专栏持续时间。 这是我的代码

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).

它似乎不理解这种格式...不过函数绘图有效:plot(DF$Time, DF$Duration) 所以我不明白为什么 ggplot 不起作用。

有什么想法吗?

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()

reprex package (v2.0.1)

于 2021-10-08 创建