r - plot a network based on given values - Stack Overflow

I have a data frame like this:df <- data.frame(char1=c('a', 'd', 'i',

I have a data frame like this:

df <- data.frame(char1=c('a', 'd', 'i', 'k', 'm', 'd', 'e', 'e', 'q', 'i','q', 'm','n','i'), 
    char2=c('b','a','a','g','n','b', 'o', 'g', 'p', 'b','g','b','k','d'),
 value=c(0.8,0.6,0.6,0.5,0.5,0.4,0.3,0.2,0.1,0.1,0.4,-0.1,-0.2,-0.3))

I want to plot a network in which each character is a node and the value between each node and another is represented by a link. The higher the value, the closer the link. And the nodes that have high values with each other, such as node 'a', 'b', and 'd' can have a cluster plot showing their strong relations.

I have a data frame like this:

df <- data.frame(char1=c('a', 'd', 'i', 'k', 'm', 'd', 'e', 'e', 'q', 'i','q', 'm','n','i'), 
    char2=c('b','a','a','g','n','b', 'o', 'g', 'p', 'b','g','b','k','d'),
 value=c(0.8,0.6,0.6,0.5,0.5,0.4,0.3,0.2,0.1,0.1,0.4,-0.1,-0.2,-0.3))

I want to plot a network in which each character is a node and the value between each node and another is represented by a link. The higher the value, the closer the link. And the nodes that have high values with each other, such as node 'a', 'b', and 'd' can have a cluster plot showing their strong relations.

Share Improve this question edited Mar 6 at 17:41 Ben Bolker 228k26 gold badges400 silver badges493 bronze badges asked Mar 6 at 17:20 user21390049user21390049 1736 bronze badges 5
  • 1 a quick search of ggplot2 network suggests the ggnet or ggnetwork packages might be useful for you – r2evans Commented Mar 6 at 17:24
  • 2 I think it's a little unlikely that you'll get a featureful all-code-included answer here on SO, which tends to be more focused on specific problems with code, this site is not well framed to be a howto/tutorial site, and some users can be a bit more against users trying to make it so. Please have a thick skin, it is possible this question will either be ignored as such, or it could be closed as "needing details" or similar close-excuses. Closing a question isn't hostile though it may feel that way. Try the pkgs above, come back with questions that use them if you can't get it to work. – r2evans Commented Mar 6 at 17:28
  • Thank you @r2evans for this! I have tried with ggraph but doesn't work so would want to ask for help if possible. Anw, will try with ggnet/ggnetwork! – user21390049 Commented Mar 6 at 17:30
  • Many thanks for your help @Ben Bolker! The negative weights have to be there so if I exponentiate it, it may give the impression that the value is high, though. – user21390049 Commented Mar 6 at 18:05
  • 1 I think a question that includes what you've attempted with ggraph would be better structured for SO, targeting more specific help. If the issue is that ggraph is known not to support what you want, then at a minimum it can demonstrate how network-plotting is not currently meeting your hopes/expectations. As a side-effect, it clearly identifies that you've done research, used packages, and tried something, before coming to SO. A question devoid of that effort is occasionally received poorly as a "code this for me" request. – r2evans Commented Mar 6 at 18:05
Add a comment  | 

1 Answer 1

Reset to default 4

Here's a start, based on this question (I exponentiated the weight values given because I think the weights have to be positive: you could modify this if you wanted)

library(igraph)
g <- graph_from_data_frame(df)
L <- layout_with_fr(g, weights = exp(df$value))
plot(g, layout = L)

It's not clear what your other options are for negative weights; as far as I can see there aren't any igraph layout options that allow for negative weights; most of those algorithms assume that the attractive force between nodes is proportional to the strength of their (positive) weights. Negative weights could in principle allow for repulsion, but I don't think that's implemented. Speaking subjectively, I don't see the problem with exponentiating; nodes connected by negative weights are still farther apart than those connected by positive weights (since exponentiation maps negative weights to values between 0 and 1).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744959518a4603403.html

相关推荐

  • r - plot a network based on given values - Stack Overflow

    I have a data frame like this:df <- data.frame(char1=c('a', 'd', 'i',

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信