R语言 重新排列图形上的点

yqyhoc1h  于 2023-02-01  发布在  其他
关注(0)|答案(2)|浏览(156)
    • bounty将在5天后过期**。回答此问题可获得+50的声誉奖励。stats_noob正在寻找来自声誉良好来源的答案:你好!有人能告诉我如何在R中使用visnetwork吗?谢谢!

我正在使用R编程语言。
我有以下数据集:

library(igraph)
library(visNetwork)

nodes_df = structure(list(id = c("Boss", "TeamA", "TeamB", "SubteamA1", 
"SubteamA2", "SubteamA3", "SubteamB1", "SubteamB2", "SubteamB3", 
"employee1", "employee2", "employee3", "employee4", "employee5", 
"employee6", "employee7", "employee8", "employee9", "employee10", 
"employee11", "employee12", "employee13", "employee14", "employee15", 
"employee16", "employee17", "employee18"), label = c("Boss", 
"TeamA", "TeamB", "SubteamA1", "SubteamA2", "SubteamA3", "SubteamB1", 
"SubteamB2", "SubteamB3", "employee1", "employee2", "employee3", 
"employee4", "employee5", "employee6", "employee7", "employee8", 
"employee9", "employee10", "employee11", "employee12", "employee13", 
"employee14", "employee15", "employee16", "employee17", "employee18"
), group = c("yellow", "red", "red", "green", "green", "green", 
"green", "green", "green", "purple", "purple", "purple", "purple", 
"purple", "purple", "purple", "purple", "purple", "purple", "purple", 
"purple", "purple", "purple", "purple", "purple", "purple", "purple"
)), row.names = c(NA, -27L), class = "data.frame")

edges_df = structure(list(from = c("Boss", "TeamA", "TeamA", "TeamA", "SubteamA1", 
"SubteamA1", "SubteamA1", "SubteamA2", "SubteamA2", "SubteamA2", 
"SubteamA3", "SubteamA3", "SubteamA3", "Boss", "TeamB", "TeamB", 
"TeamB", "SubteamB1", "SubteamB1", "SubteamB1", "SubteamB2", 
"SubteamB2", "SubteamB2", "SubteamB3", "SubteamB3", "SubteamB3"
), to = c("TeamA", "SubteamA1", "SubteamA2", "SubteamA3", "employee1", 
"employee2", "employee3", "employee4", "employee5", "employee6", 
"employee7", "employee8", "employee9", "TeamB", "SubteamB1", 
"SubteamB2", "SubteamB3", "employee10", "employee11", "employee12", 
"employee13", "employee14", "employee15", "employee16", "employee17", 
"employee18")), row.names = c(NA, -26L), class = "data.frame")

我用这些数据制作了下面的网络图:

# Create the visNetwork object
    visNetwork(nodes_df, edges_df) %>%   visHierarchicalLayout(direction = "UD") %>%
        visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
        visInteraction(navigation = "zoom") %>%
        visInteraction(navigation = "drag")

    • 我的问题:**我可以做些什么来让这个图网络更好地反映我的数据中的"层次结构"?例如,我希望"老板"出现在顶部,"团队"出现在后面,"子团队"出现在后面,"员工"出现在后面。

我试着用visHierarchicalLayout(direction = "UD")选项来解决这个问题,但我似乎无法在可视化中反映出这个层次结构的顺序。有人知道我可以做些什么来解决这个问题吗?
谢谢!

c9qzyr3d

c9qzyr3d1#

你过于关注visGraph,你不需要它来做你想做的事情。
下面是一个演示,使用上面nodes_dfedges_df的定义,与我链接的示例相比,唯一的变化是根节点的标签发生了变化。

library(igraph)

# Create the graph
g <- graph_from_data_frame(edges_df, directed=TRUE, vertices=nodes_df)
# Define the layout
ly <- layout.reingold.tilford(g, root=which(V(g)$name=='Boss'), flip.y=T)
# Plot the graph
plot(g,layout=ly)

给予

yx2lnoni

yx2lnoni2#

您可以创建一个名为level的列,每个组都有一个唯一的订单号,如老板将是编号1等。因此,当使用“从上到下”布局(direction = "UD")时,您的老板将是顶部,最后一个编号4将是布局的底部,这是员工。以下是一个可复制的示例:

library(igraph)
library(visNetwork)
library(dplyr)

#create level column
nodes_df <- nodes_df %>%
  mutate(level = case_when(group == "yellow" ~ 1,
                           group == "red" ~ 2,
                           group == "green" ~ 3,
                           group == "purple" ~ 4))

# Create the visNetwork object
visNetwork(nodes_df, edges_df) %>%   
  visHierarchicalLayout(direction = "UD") %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
  visInteraction(navigation = "zoom") %>%
  visInteraction(navigation = "drag")

创建于2023年1月30日,使用reprex v2.0.2

相关问题