I have a database with all the users and their roles, i've made a table that has usera, role, userb. i'm trying to give each user a color based on their team.
the SQL code below can be used to create a similar dataset
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)
This results in a graph with white vertexes and legend.
I've seen this post: Generate colors based on unique entries in another list in R
Resulting in the following code
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
and it generates the following error
Warning message: In brewer.pal(length(unique(V(net)$Team)), "Accent") : n too large, allowed maximum for palette Accent is 8 Returning the palette you asked for with that many colors Blockquote
i found a fix for that problem here; https://www.r-bloggers.com/how-to-expand-color-palette-with-ggplot-and-rcolorbrewer/
and this is what i made
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
I have a colored (but messy) graph but my legend didn't change color, how do i fix this.
This line of code added to the legenda only colors the border of the circle infront of it and shows a weird colored square.
fill=V(net)$color,col=V(net)$color
str(users)
'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" ...
来源:https://stackoverflow.com/questions/50602287/r-color-vertex-igraph
