【问题标题】:R: how to change the color of a point on a graphR:如何更改图形上点的颜色
【发布时间】:2020-11-23 19:02:15
【问题描述】:

我用以下数据创建了一个图网络:

#relationship 数据

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


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)

plot(graph)

nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(graph, what="edges")[1:2]

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

在 visnetwork 中,有人知道如何将“John”的颜色更改为红色吗?

我知道如何在 igraph 中做到这一点:

V(graph)["John"]$color<-"red"

但是有人知道如何在 visnetwork 中做到这一点吗?

谢谢

【问题讨论】:

    标签: r plot graph data-visualization


    【解决方案1】:

    您可以向节点添加color 列:

    nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
    nodes <- nodes[order(nodes$id, decreasing = F),]
    
    John_ind <- which(nodes$id == "John")
    colours <- rep("lightblue", nrow(nodes)); colours[John_ind] <- "red"
    nodes <- cbind(nodes, color = colours)
    edges <- get.data.frame(graph, what="edges")[1:2]
    
    visNetwork(nodes, edges) %>%   visIgraphLayout(layout = "layout_with_fr") %>%
      visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
    

    您可以相应地自定义自己的颜色。

    编辑

    对于您的评论,您输入了一个错误的名称 (Xavier),您应该注意 color 列的大小写。我们可以使用 dplyr 包中的merge()left_join 来基于列合并数据帧。我将您的 DF 列名称更改为 id 的名称,以便它们可以匹配。

    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)
    

    【讨论】:

    • 非常感谢!但是假设现在有一个完整的数据框,每个人都有颜色。有没有一种快速的方法可以将这些颜色添加到 visnetwork 中?例如颜色 = data.frame(“人” = c(“约翰”、“彼得”、“蒂姆”、“凯文”、“亚当”、“Xacier”、“克劳德”、“亨利”)、“颜色” = c (“红”、“蓝”、“绿”、“黑”、“红”、“蓝”、“黑”、“蓝”))
    • @Noob 我添加了一些代码。小心错别字!如果这回答了您的问题,请确保接受它(:
    • 我想出了如何“按颜色选择”节点$color = colors$color visNetwork(nodes, edges, main = "test", width = "100%") %>% visIgraphLayout(layout = "layout_with_fr") %>% visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>% visOptions(selectedBy = "colors$color") 是否有可能以某种方式有两个选择栏?一个颜色和ID?感谢您的所有帮助!
    • @Noob 也许您可以在文档中找到答案:rdocumentation.org/packages/visNetwork/versions/2.0.9/topics/…。尝试看看你是否能理解包文档中提到的细节是一个很好的做法。如果您仍然遇到问题,您可能需要开始一个新问题,因为我无法根据您的代码重现结果。
    • 感谢您的帮助和您的想法。我已经开始阅读文档。同时,我提出了一个新问题:stackoverflow.com/questions/64978594/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    相关资源
    最近更新 更多