0

I'm struggling with some social network mapping with R using igraph. I would like to produce a plot showing the relationships of local representative (e.g. Bill) with individuals and administrative bodies in their area. I've been able to plot vertices for Bill and his contacts with edges using the following in a graph.data.frame:

who contact     weight  associate
Bill    district    1   y
Bill    region      2   n
Bill    village A   1   y
Bill    village B   2   n
Bill    social worker   1   n
Bill    steve       1   y
Bill    church      2   n
Bill    jane        1   y
Bill    village A Admin 1   n
Bill    village B Admin 1   n

I would like to differentiate the vertices by color as to whether or not they are one of Bill's associates. I tried

V(g)$color <- ifelse(V(g) 

but get either an error message or nothing changes. I would also like to differentiate the edges in different colors by weight to represent between Bill's direct or indirect contacts using the weight variable.

Any guidance on how I can enhance Bill's plot or whether I should be using a different dataframe would be greatly appreciated.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
Curious56
  • 51
  • 1
  • 3
  • Please post enough code to reproduce what you're doing. `V(g)$color <- ifelse(V(g)` of course gives an error because it's missing a `)` and is only passing one argument to the `ifelse` function, which takes 3 arguments. – josliber Jun 26 '14 at 18:37
  • Sorry for no providing enough information. I've been trying: > V(g )$color-ifelse ( V ( g ) $ associate == "y" , " green " , " orange " ) > plot(g, – Curious56 Jun 26 '14 at 18:52

1 Answers1

1

The reason why your "associate" code isn't working is that when you do graph.data.frame the extra attributes are stored with the edges you supply, not the vertices. Here's how you can add the vertex information (And plot the weights using @jlhoward's suggestion)

#sample data    
dd <- structure(list(who = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "Bill", class = "factor"), contact = c("district", 
"region", "villageA", "villageB", "socialworker", "steve", "church", 
"jane", "villageAAdmin", "villageBAdmin"), weight = c(1L, 2L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L), associate = c("y", "n", "y", 
"n", "n", "y", "n", "y", "n", "n")), .Names = c("who", "contact", 
"weight", "associate"), row.names = c(NA, -10L), class = "data.frame")

and now the code

library(igraph)
gg <- graph.data.frame(dd[,1:3])

V(gg)[dd$contact]$associate <- dd$associate
V(gg)$color <- ifelse(V(gg)$associate=="y", "green","orange")

plot(gg,edge.color=E(gg)$weight)

which produces

output

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Sorry but I've tried applying this solution a number of times (and with a number of variations) but when I enter: V(gg)[dd$contact]$associate <- dd$associate I always get: Error in `[.igraph.vs`(`*tmp*`, dd$contact) : invalid indexing of vertex seq Plotting the weights with @jlhoward's suggestion works great though (much apprectiated). – Curious56 Jul 08 '14 at 13:30
  • @Curious56 Make sure your `contact` column is a character and not a factor. I've added a data.frame that you can copy and paste to recreate the output. – MrFlick Jul 08 '14 at 13:36
  • Mr Flick thanks ever so for getting back. By data.frame do you mean the code below #sample data? – Curious56 Jul 08 '14 at 15:52
  • @Curious56 Yes. If you copy/paste that into R it should make a data.frame called `dd` with the way i read your data in (without factors) – MrFlick Jul 08 '14 at 15:56
  • Great - it works! Thanks again and yes I do need to find out more about data.frame construction. – Curious56 Jul 08 '14 at 16:04
  • There are lots of ways to read in data. Doing a `dput()` of your data.frame lets everyone know exactly what `class()` each column is. I may have written different code if I knew you were using factors. There are tips for making reproducible data sets listed as answers to [this question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jul 08 '14 at 16:07