visNetwork / igraph的数据框

时间:2018-05-14 17:28:47

标签: r igraph visnetwork

我正在处理一些运输数据,其中包含有关船舶航行的信息。每次航行都有自己的身份证,通常一艘船在某个港口开始航行,在相同或不同的港口装载货物,然后在某个港口卸货。有时,在装载或卸载货物之前,船舶也可能必须在港口等待。此外,船舶可以在多个港口卸货。我的数据集的前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中的visNetworkigraph个包将这些信息可视化为网络图。但是为了做到这一点,我想将PortName变量分成FromTo变量并维护端口功能的属性,即。开始,加载,等待或放电。我徒劳地尝试使用for循环来迭代每个唯一的Voyage ID来实现这一点。不值得分享破碎的代码。任何指针或建议都会有所帮助。

2 个答案:

答案 0 :(得分:1)

我将您在问题中提供的数据加载到名为Voyage的data.frame中。假设您的数据按照示例中的顺序排序(行程的某些部分是连续的并且按顺序排列),我们只需要链接与下一行具有相同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)

Shipping Routes

答案 1 :(得分:0)

以下强制按Fkey_Dim_Voyage_Idord_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时会发生什么。

Network Graph