R:按颜色和ID选择 [英] R: select by color and by ID

查看:84
本文介绍了R:按颜色和ID选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有以下数据

  Data_I_Have<-data.frame("Node_A"= c("John","John","John","Peter","Peter","Peter","Tim","Kevin","Adam","Adam").亚当",泽维尔"),"Node_B";= c(克劳德",彼得",蒂姆",蒂姆",克劳德",亨利",凯文",克劳德",蒂姆",亨利",克劳德"),"Place_Where_他们_Met"= c(芝加哥",波士顿",西雅图",波士顿",巴黎",巴黎",芝加哥",伦敦",芝加哥",伦敦",巴黎"),"年份_他们_拥有_知识_其他_"= c("10","10","1","5","2","8","7","10","3","3","5")," What_You_Have_In_Common"= c(运动",电影",计算机",计算机",视频游戏",运动",电影",计算机",运动",";运动",视频游戏"))Additional_data_about_people<-data.frame(人"= c("John","Peter","Tim","Kevin","Adam","Xacier","Claude","Henry"),工作"= c(教师",律师",会计",工程师",教师",律师",工程师",律师"),年龄"= c("50","51","61","56","65","65","54","50"),收藏夹食物"= c(比萨饼",比萨饼",炸玉米饼",比萨饼",冰淇淋",​​寿司",寿司",比萨饼")) 

使用此信息,我创建了一个交互式图形,您可以在其中通过ID选择"

 库(igraph)图书馆(dplyr)图书馆(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.data.frame(graph_file,向= F)图<-简化(图)节点<-data.frame(id = V(graph)$ name,title = V(graph)$ name)节点<-节点[order(nodes $ id,减少= F),]颜色= data.frame("id" = c("John","Peter","Tim","Kevin","Adam","Xavier","Claude","Claude",亨利"),颜色"= c(红色",蓝色",绿色",黑色",红色",蓝色",黑色",蓝色")))节点<-合并(节点,颜色,按="id"表示)边缘<-get.data.frame(graph,what ="edges")[1:2]visNetwork(节点,边)%>%visIgraphLayout(layout ="layout_with_fr")%&%visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE) 

可以修改代码,以便您可以通过颜色"进行选择.而不是使用"id":

 节点$ color = colors $ color节点<-data.frame(id = V(graph)$ name,title = V(graph)$ name)节点<-节点[order(nodes $ id,减少= F),]颜色= data.frame("id" = c("John","Peter","Tim","Kevin","Adam","Xavier","Claude","Claude",亨利"),颜色"= c(红色",蓝色",绿色",黑色",红色",蓝色",黑色",蓝色")))节点<-合并(节点,颜色,按="id"表示)边缘<-get.data.frame(graph,what ="edges")[1:2]visNetwork(节点,边缘,主要=一个非常简单的示例",宽度="100%")%>%visIgraphLayout(layout ="layout_with_fr")%&%visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)%&%;%visOptions(selectedBy ="color") 

是否有将两者结合在一起的方法?您可以有两个搜索栏,一个用于ID,一个用于颜色吗?

  visNetwork(节点,边线,主要=一个非常简单的示例",宽度="100%")%>%visIgraphLayout(布局="layout_with_fr")%&%visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)%&%;%visOptions(selectedBy ="color")visNetwork(节点,边缘,主要=一个非常简单的示例",宽度="100%")%>%visIgraphLayout(layout ="layout_with_fr")%&%visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)%&%;%visLegend() 

类似这样的内容:如果要添加图例,也许您应该重新配置数据框并检查@Daman深刻评论的链接.

I have the following data in 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")
)

Using this information, I created an interactive graph where you can "select by 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)

The code can be modified so that you can select by "color" instead of by "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") 

Is there a way to combine both of these together? Can you have two search bars, one for ID and one for color?

    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()

Something like this: https://imgur.com/a/Y9yY0Q7

Thanks

解决方案

Try changing the last part of your code to

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

It results in If you want to add a legend, perhaps you should reconfigure your dataframe and check the link that @Daman deep commented.

这篇关于R:按颜色和ID选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆