如何更改节点的颜色? [英] How to change the colour of nodes?

查看:140
本文介绍了如何更改节点的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据月份更改节点的颜色.目前,我已经用这种方式完成了,这根本不够简洁,也不适用于大型数据集.有更好的方法吗?

I'm trying to change the colour of the nodes dependent upon the month. At the moment, I've done it this way which isn't concise at all and wouldn't work for large datasets. Is there a better way of doing it?

Data <- read.csv(.....) 

library(igraph)   

MartixData <- as.matrix(Data)

NetworkData <- graph.adjacency(MatrixData, mode="directed", weighted=TRUE)

V(NetworkData)

V(NetworkData)$month <- c("June", "July", "July", "February", "September", "June", "September", "June", "December", "September", "March", "April", "September")

plot(NetworkData, layout=layout.circle, vertex.color=c("yellow", "red", "red", "blue", "pink","yellow", "pink", "yellow", "gray", "pink", "black", "orange", "pink"))

任何帮助将不胜感激!

#Results for dput(NetworkData)
structure(list(13, TRUE, 
  c(0, 1, 1, 2, 5, 6, 7, 7, 8, 9, 10, 10, 12, 12, 12, 12, 12), 
  c(11, 4, 12, 0, 12, 1, 6, 12, 1, 4, 9, 12, 1, 5, 7, 10, 11), 
  c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), 
  c(3, 5, 8, 12, 1, 9, 13, 6, 14, 10, 15, 0, 16, 2, 4, 7, 11), 
  c(0, 1, 3, 4, 4, 4, 5, 6, 8, 9, 10, 12, 12, 17), 
  c(0, 1, 4, 4, 4, 6, 7, 8, 9, 9, 10, 11, 13, 17), 
  list(c(1, 0, 1), structure(list(), .Names = character(0)), 
 structure(list(name = c("A", "B", "C", "D", "E", "F", 
    "G", "H", "I", "J", "K", "L", "M"), month = c("June", 
    "July", "July", "February", "September", "June", "September", 
    "June", "December", "September", "March", "April", "September"
    )), .Names = c("name", "month")), structure(list(weight = c(6, 
    6, 7, 6, 7, 6, 6, 7, 7, 7, 6, 7, 7, 7, 7, 7, 7)), .Names = "weight"))), class = "igraph")

推荐答案

对颜色使用查找字典并将其分配给顶点颜色.

Use lookup dictionary for colours and assign it to vertex colour.

myCols <- setNames(c("yellow", "red", "blue", "pink", "gray", "black", "orange"),
                   c("June", "July", "February", "September", "December", "March", "April"))

# assign a colour for vertex
V(NetworkData)$color <- myCols[V(NetworkData)$month]

# then plot, no need to use "vertex.color="
plot(NetworkData, layout = layout.circle)

这篇关于如何更改节点的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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