我嘗試用 ggplotly 繪制這種 DF :
| 時間 | 期間 |
|---|---|
| 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()
回傳:
警告訊息:洗掉了 112 行包含缺失值 (geom_point)。
似乎它不理解這種格式......不過函式 plot 有效:plot(DF$Time, DF$Duration)所以我不明白為什么 ggplot 不起作用。
任何的想法?
uj5u.com熱心網友回復:
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 包(v2.0.1)于 2021 年 10 月 8 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315901.html
