1

我正在处理一些航运数据,其中包含有关船舶航行的信息。每个航次都有自己的 ID,通常一艘船在某个港口开始航行,在同一个或不同的港口装载货物,然后在某个港口卸货。有时,船舶可能还必须在港口等待装卸货物。此外,船舶可以在多个港口卸货。我的数据集的前 10 行如下:

structure(list(Fkey_Dim_Voyage_Id = c(1228L, 1228L, 1228L, 1656L, 
1656L, 1656L, 1675L, 1675L, 1675L, 1675L), Imos_VoyNo = c(19L, 
19L, 19L, 16L, 16L, 16L, 7L, 7L, 7L, 7L), ord_no_int = c(100, 
300, 500, 100, 200, 700, 100, 300, 500, 600), Fkey_Dim_PortFunction_Id = c("Commencing", 
"Loading", "Discharging", "Commencing", "Loading", "Discharging", 
"Commencing", "Loading", "Discharging", "Discharging"), PortName = c("CHESAPEAKE", 
"CHESAPEAKE", "IMMINGHAM", "BELLEDUNE", "PORT CARTIER", "IMMINGHAM", 
"AUGHINISH", "NORFOLK", "LA SPEZIA", "FUSINA"), Fkey_Dim_Vessel_Id = c(1179L, 
1179L, 1179L, 144L, 144L, 144L, 1124L, 1124L, 1124L, 1124L), 
    Date_Arrival = structure(c(978307200, 1511927640, 1513400760, 
    978307200, 1510578000, 1511956800, 978307200, 1511611260, 
    1513713600, 1515225600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Date_Departure = structure(c(1511945640, 1512267840, 1516425480, 
    1510489380, 1510765200, 1513359300, 1510665120, 1512283320, 
    1514903400, 1515664800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Latitude = c(36.45, 36.45, 53.38, 47.54, 50, 53.38, 52.38, 
    36.51, 44.06, 45.25), Longitude = c(-76.15, -76.15, -0.11, 
    -65.45, -66.47, -0.11, -9.03, -76.18, 9.49, 12.16)), row.names = c(NA, 
-10L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = c("Fkey_Dim_Voyage_Id", 
"Imos_VoyNo"), drop = TRUE, indices = list(0:2, 3:5, 6:9), group_sizes = c(3L, 
3L, 4L), biggest_group_size = 4L, labels = structure(list(Fkey_Dim_Voyage_Id = c(1228L, 
1656L, 1675L), Imos_VoyNo = c(19L, 16L, 7L)), row.names = c(NA, 
-3L), class = "data.frame", vars = c("Fkey_Dim_Voyage_Id", "Imos_VoyNo"
), drop = TRUE))

我正在尝试使用 R 中的visNetworkorigraph包将这些信息可视化为网络图。但是为了做到这一点,我想将PortName变量分成 FromTo变量并维护端口函数的属性,即。开始、装载、等待或卸货。for我试图通过遍历每个唯一的 Voyage ID来使用循环来实现这一点,但徒劳无功。不值得分享损坏的代码。任何指示或建议都会有所帮助。

4

2 回答 2

1

我将您在问题中提供的数据加载到名为 的 data.frame 中Voyage。假设您的数据按照示例进行排序(行程的部分是连续且按顺序排列的),我们只需要链接与Fkey_Dim_Voyage_Id下一行相同的记录的端口。

library(igraph)

Leg = which(Voyage$Fkey_Dim_Voyage_Id[-1] == head(Voyage$Fkey_Dim_Voyage_Id, -1))

FROM = Voyage$PortName[Leg]
TO   = Voyage$PortName[Leg+1]

Routes = data.frame(FROM, TO)

G = graph_from_data_frame(Routes)
plot(G)

运输路线

于 2018-05-14T19:03:58.977 回答
0

以下强制进行预排序,Fkey_Dim_Voyage_Id并且ord_no_int应该能够生成干净的图形:

library(ggraph)
library(tidygraph)
library(tidyverse)

df %>%
  group_by(Fkey_Dim_Voyage_Id) %>%
  arrange(Fkey_Dim_Voyage_Id, ord_no_int) %>%
  mutate(from = PortName,
         to = lead(PortName)) %>%
  rename(PortFunction = Fkey_Dim_PortFunction_Id) %>%
  as_tbl_graph(directed = TRUE) %>%
  ggraph(., layout = 'linear', circular = TRUE) +
  geom_edge_fan(aes(start_cap = circle(4, 'mm'),
                    end_cap = circle(4, 'mm'),
                    color = PortFunction),
                 arrow = arrow(length = unit(4, 'mm'))) +
  geom_node_text(aes(label = name), size = 3) +
  geom_edge_loop(aes(color = PortFunction),
                 arrow = arrow(length = unit(4, 'mm'))) +
  coord_fixed(ratio = 3/5) +
  theme_graph()

通过利用tidygraph+ ggraph,您将可以灵活地表示节点和边并为其添加美感。例如,看看当您从 更改为 时会发生geom_edge_fan什么geom_edge_link

网络图

于 2018-05-14T19:28:39.267 回答