向图节点添加标签

Adding Labels to Graph Nodes

我使用“visnetwork”库制作了下图:

library(tidyverse)
library(igraph)


set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))

relations = data.frame(tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
))

data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )

graph = graph_from_data_frame(relations, directed=T, vertices = data) 


 #red circle: starting point and final point
V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2, main = "my_graph")

library(visNetwork)

    a = visIgraph(graph)  

m_1 = 1
m_2 = 23.6

 a = toVisNetworkData(graph) %>%
    c(., list(main = paste0("Trip ", m_1, " : "), submain = paste0 (m_2, "KM") )) %>%
    do.call(visNetwork, .) %>%
    visIgraphLayout(layout = "layout_in_circle") %>% 
    visEdges(arrows = 'to') 

我试图在数据文件中添加一个额外的列来包含这些数字(每个“数字标签”对应于访问城市的顺序):

#is there an "automatic" way to do this? i did this manually:
data$label = c(11, 5, 2, 12, 7, 6, 10, 14, 15, 4, 12, 9, 13, 3,1)

然后我尝试将“数字标签”和“名称标签”(即城市)添加到 visnetwork 图中的节点:

V(graph)$name = data$label = c(11, 5, 2, 12, 7, 6, 10, 14, 15, 4, 12, 9, 13, 3,1)

plot(graph, layout=layout.circle, edge.arrow.size = 0.2, main = "my_graph")

library(visNetwork)

    a = visIgraph(graph)  

m_1 = 1
m_2 = 23.6

 a = toVisNetworkData(graph) %>%
    c(., list(main = paste0("Trip ", m_1, " : "), submain = paste0 (m_2, "KM") )) %>%
    do.call(visNetwork, .) %>%
    visIgraphLayout(layout = "layout_in_circle") %>% 
    visEdges(arrows = 'to') 

a

但是它给我一个错误:

Error in `.rowNamesDF<-`(x, value = value) : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique value when setting 'row.names': ‘12’ 

最后还是想做这个图(使用visnetwork):

还有其他方法吗?

谢谢!

错误原因是没有8,有2个12

sort(c(11, 5, 2, 12, 7, 6, 10, 14, 15, 4, 12, 9, 13, 3, 1))
# [1]  1  2  3  4  5  6  7  9 10 11 12 12 13 14 15
# missing 8, two 12

动态获取订单--(这使用您在上面的代码中创建的第一个 a 对象)

library(tidyverse)

# collect the correct order

df2 <- data %>% 
  mutate(d = as.numeric(d),
         nuname = factor(a$x$edges$from, 
                         levels = unlist(data$name))) %>%
  arrange(nuname) %>% 
  select(d) %>% unlist(use.names = F)
#  [1] 11  5  2  8  7  6 10 14 15  4 12  9 13  3  1 

如果您在此处编写代码时设置标签,则会覆盖您当前拥有的标签。

有几种选择:保持原样、组合标签或您知道我不知道的东西。

使用这些值和您的代码:

V(graph)$name = data$label = df2

给你:

要合并标签,您可以:

V(graph)$name = data$label = paste0(df2, "\n", data$name)

这给你:

在我看来,您的代码过于复杂,让人难以理解您的方法的整体逻辑。所以我试着减轻一点并更好地组织它。

请在下面找到生成您想要的图形所需的完整代码。

注意:按照惯例,我们在描述 graph 时通常使用术语 'nodes''edges'。这也有助于使代码更易于阅读。所以我用 'nodes' 来引用你的 'data' 数据集,用 'edges' 来引用你的 'relations' 数据集。

Reprex

  • 构建图表的代码
library(dplyr)
library(igraph)

set.seed(123)
n=15

# Build the dataframe of 'nodes'
nodes <- data.frame(d = 1:n,
                    name = c("new york", "chicago", "los angeles", "orlando", "houston", 
                             "seattle", "washington", "baltimore", "atlanta", "las vegas", 
                             "oakland", "phoenix", "kansas", "miami", "newark"))

# Build the dataframe of 'edges'
edges <- data.frame(from = sample(nodes$d)) %>% 
                      mutate(to = lead(from, default = from[1]))

# Build the graph
graph <- graph_from_data_frame(edges, directed = TRUE, vertices = nodes) %>% 
  set_vertex_attr("name", 
                  index = V(.), 
                  paste0(order(as.numeric(edges$from)), "\n", nodes$name)) %>% 
  set_vertex_attr("color",
                  index = V(.),
                  ifelse(nodes$d == edges$from[1], "red", "orange"))
  • 可视化图表的代码
library(visNetwork)

m_1 <- 1
m_2 <- 23.6

a <- toVisNetworkData(graph) %>%
  c(., list(main = paste0("Trip ", m_1, " : "), submain = paste0 (m_2, "KM") )) %>%
  do.call(visNetwork, .) %>%
  visIgraphLayout(layout = "layout_in_circle") %>% 
  visEdges(arrows = 'to') 
a

reprex package (v2.0.1)

创建于 2022-03-01