3

for a dataframe

df <- data.frame(g1=c("x1","x2","x2","x3","x4","x5","x5","x3"),
                               g2=c("y1","y4","y2","y4","y3","y4","y5","y4"), 
                               g1value=c(1,2,2,3,4,5,5,3),
                               g2value=c(2,4,2,4,5,4,NA, 4),
                               stringsAsFactors = FALSE)

library(igraph)
g=graph.data.frame(df, directed=F)
plot(g)

how shall i specify the size of nodes such that each size of x-nodes correspond to g1value,and y nodes to g2value (ignore the node if its size is na)?

and how to vary either node border colour or thickness on conditions, such as when the node is x2?

If there're many nodes, and the plotting result is crowded, anyway to space it out?

Thanks.

santoku
  • 3,297
  • 7
  • 48
  • 76

1 Answers1

2

This is a start - you can pass different vectors of sizes and colours to the arguments of plot.igraph so that they apply to the different nodes. They are given in the order of V(g)$name.

# tweaking your data to increase node size
df <- data.frame(g1=c("x1","x2","x2","x3","x4","x5","x5","x3"),
                 g2=c("y1","y4","y2","y4","y3","y4","y5","y4"), 
                 g1value=10*c(1,2,2,3,4,5,5,3),
                 g2value=10*c(2,4,2,4,5,4,NA, 4),
                 stringsAsFactors = FALSE)

library(igraph)

g <- graph.data.frame(df[,1:2], directed=F)

# create a vector of vertex sizes conditional on g*values in df
# set missing values to size 0 
r <- data.frame(g=c(df$g1, df$g2), value=c(df$g1value, df$g2value))
sz <- r$value[match(V(g)$name, r$g)]
sz[is.na(sz)] <- 0 

# create a vertor of border colours conditional on node type
bd <- ifelse(grepl("x", V(g)$name), "red", NA) 

# add the size and border colour vectors to relevant plot arguments     
plot(g, vertex.size=sz, vertex.frame.color=bd)


igraph has good help pages, see ?igraph::layout for graph layout options, ?plot.igraph and ?igraph.plotting for some plotting options/arguments.

For the border width follow this link.

Community
  • 1
  • 1
user20650
  • 24,654
  • 5
  • 56
  • 91
  • There are 10 nodes in `V(g)` and 8 rows in `df`, so the `ifelse` approach doesn't seem correct here when computing `sz`. – josliber Sep 11 '14 at 17:46
  • @josilber; updated with a rather unsatisfying attempt - please feel free to improve – user20650 Sep 11 '14 at 18:06
  • i encountered the issue of vertex overlap with each other when there're more points. is there a way to resolve this except circle layout? example: z=paste0("z",1:1000) k=rep(paste0("k",1:500),2) df=data.frame(k,z) g <- graph.data.frame(df, directed=F) sz=sample(2000) – santoku Sep 18 '14 at 14:26
  • You have 500 clusters of 3 nodes - it will be hard for any of the layout algorithms to do much. You could maybe try manually creating a layout matrix so the clusters are in a grid. You have also massively increased the size of the nodes so of course there will be overlap - at the very least the plot is limited to the size of your display. Sorry i know its not much help. You could try posting this as a new question to get some more advanced help. I do think your expectation of displaying the above graph are a bit unrealistic but would be good to see what the experts say. – user20650 Sep 18 '14 at 15:37