【问题标题】:Permutation diagram in rr中的排列图
【发布时间】:2012-08-21 11:04:23
【问题描述】:

我有一个 data.frame,显示了站点 x 的变量与站点 y 的变量的所有可能组合之间的关系强度。

set.seed(1410)
df<-data.frame(
"site.x"=c(rep("a",4),rep("b",4),rep("c",4),rep("d",4)),
"site.y"=c(rep(c("e","f","g","h"),4)),
"bond.strength"=sample(1:100,16, replace=TRUE))

   site.x site.y bond.strength
    a      e            27
    a      f            54
    a      g            94
    a      h            15
    b      e            58
    b      f            50
    b      g            67
    b      h            51
    c      e            58
    c      f             5
    c      g            48
    c      h            32
    d      e             5
    d      f            13
    d      g            84
    d      h            39

我需要一个图表,可以将 df 中的信息汇总在一个图中。我在想也许是这样的排列图.....

任何建议我将如何去做这样的事情?非常感谢。

【问题讨论】:

    标签: r permutation diagram figure


    【解决方案1】:

    这使用您的数据给出了类似的结果:

    library(igraph)
    df<-graph.data.frame(df)
    V(df)$names <- c("a","b","c","d","e","f","g","h")
    layOUT<-data.frame(x=c(rep(1,4),rep(2,4)),y=c(4:1,4:1))
    E(df)[ bond.strength < 101 ]$color <- "red"
    E(df)[ bond.strength < 67 ]$color <- "yellow"
    E(df)[ bond.strength < 34 ]$color <- "green"
    V(df)$color <- "white"
    l<-as.matrix(layOUT)
    plot(df,layout=l,vertex.size=10,vertex.label=V(df)$names,
    edge.arrow.size=0.01,vertex.label.color = "black")
    

    【讨论】:

    • 感谢您的回答!我正在尝试将其用作我的实际数据的模型。你能解释一下布局步骤吗?我知道这与节点的组织方式有关。我将如何调整它以说明站点 x 有 10 个项目而站点 y 只有 6 个的情况?谢谢
    • 嘿,layOUT 步骤只是为节点创建坐标。一旦你运行了代码类型layout 并按下回车键。您将看到它的唯一数据框,其中 x 列是节点 x 坐标,y 列是节点 y 坐标。 igraph 将这些坐标用作矩阵,因此 as.matrix 只需将其放入格式 igraph 即可使用。
    • 即使有不同数量的节点,一切仍然有效。在我的示例中,xy 的长度为 8,因为您有 8 个节点。例如,如果你有 16 个节点,你可以用这个 layOUT&lt;-data.frame(x=c(rep(1,10),rep(2,6)),y=c(10:1,10:5)) 来创建 layOUT
    • 让它工作,谢谢。抱歉,最后一个问题...如何添加图例?
    • 在您绘制之后尝试类似legend("bottomright", col=c("green","yellow","red"), c("0-33","33-66","66-100"), pch=15) 的操作。看看?legend tweeking 这个。
    【解决方案2】:

    这是一种使用基础 R 的方法,应该适用于具有相同结构的任何数据帧。请注意,我将原始示例中的因子更改为字符串。

    set.seed(1410)
    df<-data.frame(
      "site.x"=c(rep("a",4),rep("b",4),rep("c",4),rep("d",4)),
      "site.y"=c(rep(c("e","f","g","h"),4)),
      "bond.strength"=sample(1:100,16, replace=TRUE), stringsAsFactors=FALSE)
    
    
    Placement <- data.frame(site=c(unique(df$site.x),unique(df$site.y)), x = NA, y = NA, stringsAsFactors=FALSE)
    
    Placement$x <- ifelse(Placement$site %in% unique(df$site.x), 0, 1)
    
    Placement$y[Placement$x==0] <- seq(1,0,length=sum(Placement$x==0))
    Placement$y[Placement$x==1] <- seq(1,0,length=sum(Placement$x==1))
    
    cols <- rep("red",nrow(df))
    cols[df$bond.strength < 33] <- "green"
    cols[df$bond.strength >= 33 & df$bond.strength < 66] <- "yellow"
    
    # Empty plot:
    par(mar=c(0,0,0,0))
    plot(1,type="n",xlim=c(-0.2,1.6),ylim=c(0,1),bty="n",axes=FALSE,xlab="",ylab="")
    abline(v=c(0,1))
    text(Placement$x + ifelse(Placement$x==0,-0.1,0.1),Placement$y,Placement$site)
    
    for (i in 1:nrow(df))
    {
      lines(c(0,1),Placement$y[c(match(df$site.x[i],Placement$site),match(df$site.y[i],Placement$site))],col=cols[i],lwd=2)
    }
    
    legend("right",col=c("green","yellow","red"),lty=1,lwd=2,legend=c("0-33","33-66","66-100"),title="bondstrength",cex=1.5,bty="n")
    

    【讨论】:

      猜你喜欢
      • 2022-07-08
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2020-04-27
      • 2021-10-19
      • 2015-06-09
      相关资源
      最近更新 更多