briatte / ggnet

Network visualization with ggplot2
https://briatte.github.io/ggnet/
194 stars 33 forks source link

Can't set node size and group at the same time #1

Closed fzhang612 closed 11 years ago

fzhang612 commented 11 years ago

Hi,

Seems like I can set node size and group separately but can't simultaneously. Here is the code

# make a graph
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                            "Esmeralda"),
                     age=c(48,33,45,34,21),
                     gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                               "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                        same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
                        friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
g <- graph.data.frame(relations, directed=TRUE, vertices=actors)

# setting up node size alone works
ggnet(g, size=V(g)$age/2)

# setting up node group alone works as well
ggnet(g, node.group = V(g)$gender)

# this does NOT work if setting up size and group at the same time
ggnet(g, node.group = V(g)$gender, size = V(g)$age/2)

Thanks!

briatte commented 11 years ago

Thanks for the report. There are two issues here:

  1. There is a namespace conflict between network and igraph
  2. The size argument is meant to be a single-length vector

I have fixed the first issue in my last commit. I don't yet have a solution to the second one, though, because I had not planned this use of the size argument. I can try to make it work later, though :)

Hope this helps!

briatte commented 11 years ago

P.S. Your issue just helped me improve the way node labels are drawn. Thanks!

fzhang612 commented 11 years ago

Glad to make some contribution.

Looking forward to next version where I can control the node size. In my opinion this is a useful feature.

Keep up the good work. Thanks!

briatte commented 11 years ago

Here's a way to get what you want in the meantime:

ggnet(g, size = 0, node.group = V(g)$gender) + 
  geom_point(aes(size = V(g)$age/2)) + 
  scale_size_area(max_size = 12)

rplot

fzhang612 commented 11 years ago

Looks great! Thanks!

Should I close this issue?

briatte commented 11 years ago

Yes please :)