如何在 R 中实现带有开始日期、结束日期和 "middle date" 标记的时间线?
How to implement a timeline in R with start date, end date, and a marker for a "middle date"?
我们有一个 R
格式的数据框:
Type Request ID Event Name First Seen Update Last Seen
A 1 Event1 1/29/2017 19:54 4/19/2017 14:16 4/19/2017 15:05
A 2 Event2 2/13/2017 14:20 5/2/2017 12:48 5/2/2017 12:54
A 3 Event3 4/29/2017 16:30 5/12/2017 11:05 5/12/2017 12:08
B 4 Event4 5/17/2017 20:23 5/18/2017 12:46 5/18/2017 16:15
对应的CSV文件为:
Type,Request ID,Event Name,First Seen,Update,Last Seen
A,1,Event1,1/29/2017 19:54,4/19/2017 14:16,4/19/2017 15:05
A,2,Event2,2/13/2017 14:20,5/2/2017 12:48,5/2/2017 12:54
A,3,Event3,4/29/2017 16:30,5/12/2017 11:05,5/12/2017 12:08
B,4,Event4,5/17/2017 20:23,5/18/2017 12:46,5/18/2017 16:15
我们想在 R 时间轴上可视化每个实例,这样我们就可以在时间轴上看到带有开始日期、更新日期和结束日期的事件。
我们在 R 中的实现非常接近于此,如下所示:
install.packages("timevis")
library("timevis")
df <- read.csv("data.csv", header = TRUE)
df_new = rename(df, start = First.Seen, end = Last.Seen, content = Request.ID)
timevis(dataframe_new)
请注意,我们在此实施中仅使用 'start' 日期和 'end' 日期。这绘制了以下时间线:
现在我们想以某种方式将 'Update' 日期和时间合并到其中,以便在每个条形图或平板中显示一个指示更新日期和时间的指针。该栏将从开始日期开始,在结束日期结束,并在适当的位置有一个标记以显示 'Update'.
我们如何在 R 中实现它?
您的数据
df <- structure(list(Type = c("A", "A", "A", "B"), Request.ID = 1:4,
Event.Name = c("Event1", "Event2", "Event3", "Event4"), First.Seen = structure(c(1485719640,
1486995600, 1493483400, 1495052580), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Update = structure(c(1492611360, 1493729280,
1494587100, 1495111560), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Last.Seen = structure(c(1492614300, 1493729640,
1494590880, 1495124100), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), class = "data.frame", .Names = c("Type", "Request.ID",
"Event.Name", "First.Seen", "Update", "Last.Seen"), row.names = c(NA,
-4L))
tidyverse 解决方案
I melt
First.Seen
& Update
到一个单独的列中。每个Update
值的Last.Seen
值是= NA
(做成一个奇点)。我添加 type
列,指定 point
用于奇点,background
用于范围(重叠值)。我还添加了一个 group
值。
library(tidyverse)
library(reshape2)
library(lubridate)
df1 <- df %>%
nest(First.Seen, Update) %>%
mutate(data = map(data, ~melt(.x))) %>%
unnest() %>%
mutate(Last.Seen = ifelse(variable == "Update", as.character(NA), as.character(Last.Seen))) %>%
mutate(Last.Seen = ymd_hms(Last.Seen)) %>%
mutate(type = ifelse(is.na(Last.Seen), "point", "background")) %>%
mutate(group = Request.ID) %>%
rename(start = value, end = Last.Seen, content = Request.ID)
前 4 行 df1
Type content Event.Name end variable start type group
1 A 1 Event1 2017-04-19 15:05:00 First.Seen 2017-01-29 19:54:00 background 1
2 A 1 Event1 NA Update 2017-04-19 14:16:00 point 1
3 A 2 Event2 2017-05-02 12:54:00 First.Seen 2017-02-13 14:20:00 background 2
4 A 2 Event2 NA Update 2017-05-02 12:48:00 point 2
指定组以及用 groups=...
标记每行的内容
timevis(data=df1, groups=data.frame(id=unique(df1$group), content=LETTERS[unique(df1$content)]))
这会产生四行时间线,其中 Update
奇点(点)标记 First.Seen
和 Last.Seen
的每个范围。
我们有一个 R
格式的数据框:
Type Request ID Event Name First Seen Update Last Seen
A 1 Event1 1/29/2017 19:54 4/19/2017 14:16 4/19/2017 15:05
A 2 Event2 2/13/2017 14:20 5/2/2017 12:48 5/2/2017 12:54
A 3 Event3 4/29/2017 16:30 5/12/2017 11:05 5/12/2017 12:08
B 4 Event4 5/17/2017 20:23 5/18/2017 12:46 5/18/2017 16:15
对应的CSV文件为:
Type,Request ID,Event Name,First Seen,Update,Last Seen
A,1,Event1,1/29/2017 19:54,4/19/2017 14:16,4/19/2017 15:05
A,2,Event2,2/13/2017 14:20,5/2/2017 12:48,5/2/2017 12:54
A,3,Event3,4/29/2017 16:30,5/12/2017 11:05,5/12/2017 12:08
B,4,Event4,5/17/2017 20:23,5/18/2017 12:46,5/18/2017 16:15
我们想在 R 时间轴上可视化每个实例,这样我们就可以在时间轴上看到带有开始日期、更新日期和结束日期的事件。
我们在 R 中的实现非常接近于此,如下所示:
install.packages("timevis")
library("timevis")
df <- read.csv("data.csv", header = TRUE)
df_new = rename(df, start = First.Seen, end = Last.Seen, content = Request.ID)
timevis(dataframe_new)
请注意,我们在此实施中仅使用 'start' 日期和 'end' 日期。这绘制了以下时间线:
现在我们想以某种方式将 'Update' 日期和时间合并到其中,以便在每个条形图或平板中显示一个指示更新日期和时间的指针。该栏将从开始日期开始,在结束日期结束,并在适当的位置有一个标记以显示 'Update'.
我们如何在 R 中实现它?
您的数据
df <- structure(list(Type = c("A", "A", "A", "B"), Request.ID = 1:4,
Event.Name = c("Event1", "Event2", "Event3", "Event4"), First.Seen = structure(c(1485719640,
1486995600, 1493483400, 1495052580), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Update = structure(c(1492611360, 1493729280,
1494587100, 1495111560), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Last.Seen = structure(c(1492614300, 1493729640,
1494590880, 1495124100), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), class = "data.frame", .Names = c("Type", "Request.ID",
"Event.Name", "First.Seen", "Update", "Last.Seen"), row.names = c(NA,
-4L))
tidyverse 解决方案
I melt
First.Seen
& Update
到一个单独的列中。每个Update
值的Last.Seen
值是= NA
(做成一个奇点)。我添加 type
列,指定 point
用于奇点,background
用于范围(重叠值)。我还添加了一个 group
值。
library(tidyverse)
library(reshape2)
library(lubridate)
df1 <- df %>%
nest(First.Seen, Update) %>%
mutate(data = map(data, ~melt(.x))) %>%
unnest() %>%
mutate(Last.Seen = ifelse(variable == "Update", as.character(NA), as.character(Last.Seen))) %>%
mutate(Last.Seen = ymd_hms(Last.Seen)) %>%
mutate(type = ifelse(is.na(Last.Seen), "point", "background")) %>%
mutate(group = Request.ID) %>%
rename(start = value, end = Last.Seen, content = Request.ID)
前 4 行 df1
Type content Event.Name end variable start type group
1 A 1 Event1 2017-04-19 15:05:00 First.Seen 2017-01-29 19:54:00 background 1
2 A 1 Event1 NA Update 2017-04-19 14:16:00 point 1
3 A 2 Event2 2017-05-02 12:54:00 First.Seen 2017-02-13 14:20:00 background 2
4 A 2 Event2 NA Update 2017-05-02 12:48:00 point 2
指定组以及用 groups=...
timevis(data=df1, groups=data.frame(id=unique(df1$group), content=LETTERS[unique(df1$content)]))
这会产生四行时间线,其中 Update
奇点(点)标记 First.Seen
和 Last.Seen
的每个范围。