【问题标题】:R: select by color and by IDR:按颜色和ID选择
【发布时间】:2020-11-24 00:31:49
【问题描述】:

我在R中有以下数据

Data_I_Have <- data.frame(
   
    "Node_A" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
    "Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude"),
    " Place_Where_They_Met" = c("Chicago", "Boston", "Seattle", "Boston", "Paris", "Paris", "Chicago", "London", "Chicago", "London", "Paris"),
  "Years_They_Have_Known_Each_Other" = c("10", "10", "1", "5", "2", "8", "7", "10", "3", "3", "5"),
  "What_They_Have_In_Common" = c("Sports", "Movies", "Computers", "Computers", "Video Games", "Sports", "Movies", "Computers", "Sports", "Sports", "Video Games")
)

additional_data_about_people <- data.frame(
   
    "Person" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xacier", "Claude", "Henry"),
   "Job" = c("Teacher", "Lawyer", "Accountant", "Engineer", "Teacher", "Lawyer", "Engineer", "Lawyer"),
"Age" = c("50", "51", "61", "56", "65", "65", "54", "50"),
"Favorite_Food" = c("pizza", "pizza", "tacos", "pizza", "ice cream", "sushi", "sushi", "pizza")
)

使用这些信息,我创建了一个交互式图表,您可以在其中“按 ID 选择”

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


graph_file <- data.frame(Data_I_Have$Node_A, Data_I_Have$Node_B)


colnames(graph_file) <- c("Data_I_Have$Node_A", "Data_I_Have$Node_B")

graph <- graph.data.frame(graph_file, directed=F)
graph <- simplify(graph)


nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
nodes <- nodes[order(nodes$id, decreasing = F),]

colors = data.frame( "id" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier", "Claude", "Henry"), 
                     "color" = c("red", "blue", "green", "black", "red", "blue", "black", "blue") )

nodes <- merge(nodes, colors, by = "id")
edges <- get.data.frame(graph, what="edges")[1:2]

visNetwork(nodes, edges) %>%   visIgraphLayout(layout = "layout_with_fr") %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)

可以修改代码,以便您可以通过“颜色”而不是“id”进行选择:

 nodes$color = colors$color
 nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
 nodes <- nodes[order(nodes$id, decreasing = F),]
 
 colors = data.frame( "id" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier", "Claude", "Henry"), 
                      "color" = c("red", "blue", "green", "black", "red", "blue", "black", "blue") )
 
 nodes <- merge(nodes, colors, by = "id")
 edges <- get.data.frame(graph, what="edges")[1:2]
 
 visNetwork(nodes, edges,  main = "A really simple example", width = "100%") %>%   visIgraphLayout(layout = "layout_with_fr") %>%
     visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>% visOptions(selectedBy = "color") 

有没有办法将这两者结合在一起?你能有两个搜索栏,一个用于 ID,一个用于颜色吗?

    visNetwork(nodes, edges,  main = "A really simple example", width = "100%") %>%   visIgraphLayout(layout = "layout_with_fr") %>%
     visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>% visOptions(selectedBy = "color") 

visNetwork(nodes, edges,  main = "A really simple example", width = "100%") %>%   visIgraphLayout(layout = "layout_with_fr") %>%
    visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
    visLegend()

类似这样的:https://imgur.com/a/Y9yY0Q7

谢谢

【问题讨论】:

  • 你看到this了吗?
  • 是的,但不幸的是它并没有解决问题
  • @Noob 你自己运行了这段代码吗?我收到错误消息:'In visOptions(., selectedBy = "colors$color") : Can't find 'colors$color' in node data.frame'。
  • @PLY :很抱歉,有一个错误。我想我现在修好了。谢谢

标签: r graph data-visualization nodes edges


【解决方案1】:

尝试将代码的最后一部分更改为

visNetwork(nodes, edges, main = "test", width = "100%") %>% 
  visIgraphLayout(layout = "layout_with_fr") %>% 
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE,
             selectedBy = list(multiple = TRUE, variable = "color"))

结果 如果您想添加图例,也许您应该重新配置您的数据框并检查@Daman deep 评论的链接。

【讨论】:

  • 谢谢!是否可以添加第三个下拉菜单?
  • d = data.frame("id" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier", "Claude", "Henry "), "color" = c("r", "b", "g", "b", "r", "b", "b", "b") ) 节点 % visIgraphLayout(layout = "layout_with_fr") %>% visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>% visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE, selectedBy = list(multiple = TRUE, variable = "color" ))
猜你喜欢
  • 1970-01-01
  • 2013-08-13
  • 2021-01-11
  • 2014-12-17
  • 2021-09-30
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2021-05-29
相关资源
最近更新 更多