R颜色顶点igraph

时间:2018-05-30 10:24:41

标签: r igraph

我有一个包含所有用户及其角色的数据库,我创建了一个包含usera,role,userb的表。我试图根据他们的团队给每个用户一个颜色。

下面的SQL代码可用于创建类似的数据集

select concat('H',ABS(Checksum(NewID()) % 999999)) acentralacc, CONCAT('Role_',CHAR( FLOOR(65 + (RAND() * 25))))role,concat('H',ABS(Checksum(NewID()) % 999999))bcentralacc
select concat('H',ABS(Checksum(NewID()) % 999999)) centralaccountname, CONCAT('Team_',CHAR( FLOOR(65 + (RAND() * 25))))team

#get library's
install.packages("igraph")
library("igraph")

#edges (connection between users)
connStr <- "Driver={SQL Server};MyCon;Trusted_Connection=TRUE"
dsSqlServerData <- RxSqlServerData(sqlQuery = "SELECT top 100000 * FROM MyTable1", connectionString = connStr)
data <- rxDataStep(dsSqlServerData)
    head(data)
  acentralacc                   rol bcentralacc
1     H000062 ADDN_Basis_BBE_intern     H000079
2     H000062 ADDN_Basis_BBE_intern     H000082
3     H000062 ADDN_Basis_BBE_intern     H000092
4     H000062 ADDN_Basis_BBE_intern     H000170
5     H000062 ADDN_Basis_BBE_intern     H000197
6     H000062 ADDN_Basis_BBE_intern     H000233
data$rol <- 1
    head(data)
  acentralacc rol bcentralacc
1     H000062   1     H000079
2     H000062   1     H000082
3     H000062   1     H000092
4     H000062   1     H000170
5     H000062   1     H000197
6     H000062   1     H000233
data1 <- aggregate(data[,2],data[,-2],sum)
data1 <- data1[order(data1$acentralacc,data1$bcentralacc),]
    head(data1)
   acentralacc bcentralacc  x
1      H000062     H000062 58
9      H000062     H000067 15
17     H000062     H000071 17
25     H000062     H000073 13
33     H000062     H000077 11
41     H000062     H000079 13
#vertices (ID,node attributs)
connStr <- "Driver={SQL Server};MyCon;Trusted_Connection=TRUE"
dsSqlServerData <- RxSqlServerData(sqlQuery = "SELECT  distinct CentralAccount,isnull(Team,'No Team') Team FROM MyTable2", connectionString = connStr)
users <- rxDataStep(dsSqlServerData)
    head(users)
  CentralAccount                         Team
1        H000062        ICT Customer Services
2        H000067            Financieel Beheer
3        H000070       Acceptatie Team 2 Gent
4        H000071       Acceptatie Team 1 Gent
5        H000073 NL Ond UW & Risk Advice Auto
6        H000076                      Incasso
#parsing (converting) users team
users$Team <- iconv(users$Team, "ASCII", "UTF-8", sub="")
net <- graph_from_data_frame(d=data1, vertices=users, directed=T) 
#the following should be igraph
class(net) 
#removing graphical loops etc
net <- simplify(net, remove.multiple = F, remove.loops = T)
#creating color palet
pal2 <- rainbow(5, alpha=.5)
V(net)$color <- pal2[V(net)$Team]
plot(delete.vertices(simplify(net), degree(net)==0),vertex.size=5,vertex.label=NA,edge.arrow.size=.2)
legend("topleft", unique(c(V(net)$Team)), pch=21,col="#777777", pt.cex=2, cex=.8, bty="n", ncol=1)

这会生成带有白色顶点和图例的图形。

我看过这篇文章:Generate colors based on unique entries in another list in R

导致以下代码

colrs<- brewer.pal(length(unique(V(net)$Team)), "Accent")  
names(colrs) <- unique(V(net)$Team)
V(net)$color <- colrs[V(net)$Team]      
V(net)$color

并生成以下错误

  

警告信息:在brewer.pal(长度(唯一(V(净)$团队)),“重音”)   :n太大,允许最大调色板重音是8返回   你要求的那种颜色很多的调色板   块引用

我在这里找到了解决这个问题的方法; https://www.r-bloggers.com/how-to-expand-color-palette-with-ggplot-and-rcolorbrewer/

这就是我所做的

install.packages("RColorBrewer")
library(RColorBrewer)

colourCount = length(unique(users$Team))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))
colrs<- getPalette(colourCount)
names(colrs) <- unique(V(net)$Team)
V(net)$color <- colrs[V(net)$Team]  
V(net)$color

我有一张彩色(但很杂乱)的图表,但我的传奇并没有改变颜色,我该如何解决这个问题。

添加到图例中的这行代码只会对其前面的圆圈边框进行着色,并显示一个奇怪的彩色正方形。 fill=V(net)$color,col=V(net)$color

WeirdResult

STR(用户)

'data.frame':   2481 obs. of  2 variables:
 $ CentralAccount: chr  "H000062" "H000067" "H000070" "H000071" ...
 $ Team          : chr  "ICT Customer Services" "Financieel Beheer" "Acceptatie Team 2 Gent" "Acceptatie Team 1 Gent" ...

0 个答案:

没有答案