【问题标题】:Can qgraph render edge labels outside the actual edge?qgraph 可以在实际边缘之外渲染边缘标签吗?
【发布时间】:2016-02-23 11:27:18
【问题描述】:

出于可读性目的,我正在尝试在我的 qgraph 中的实际边缘之外插入边缘标签。我特别不喜欢在标签下方包含一个白色背景的选项,它会搞砸边缘。根据手册,可以仅沿线调整边缘标签位置,而不能在侧面调整。以前有人为此挣扎过吗?有没有可能绕过这个问题?干杯

【问题讨论】:

    标签: r graph label readability r-qgraph


    【解决方案1】:

    似乎没有用于调整边缘标签的跨轴位置的参数。一种解决方案是分别将边缘标签添加到绘图中。下面给出了一个例子,它产生了下面的图。一般的方法是获取绘图的布局,然后使用两个节点位置的平均值将文本沿线放置。可以手动调整位置,以便文本通常与线平行,但主要是偏离线(x 和 y 偏移基于角度的正弦和余弦)。如果您想进一步控制,您可以手动调整一些text() 位置以获得更好的结果。

    library(qgraph)
    
    # creating some random data
    set.seed(10)
    x1 <- rnorm(100,0,1)
    x2 <- x1 + rnorm(100,0,0.2)
    x3 <- x1 + x2 + rnorm(100,0,0.2)
    x4 <- rnorm(100,0,1)
    x5 <- x4 + rnorm(100,0,0.4)
    x6 <- x4 + rnorm(100,0,0.4)
    x7 <- x1 + x5 + rnorm(100,0,0.1)
    
    # making a data frame
    df <- cbind(x1,x2,x3,x4,x5,x6,x7)
    
    # calculating the qgraph for the correlation matrix
    # a stores the layout
    a <- qgraph(cor(df,method="pearson")
               ,layout="spring"
               ,label.cex=0.9
               ,labels=colnames(df)
               ,label.scale=F
               ,details=T
               ,edge.labels=T
               ,doNotPlot=T
               ,alpha=0.05
               ,minimum='sig'
               ,sampleSize=100)
    
    # plotting actual graph
    qgraph(cor(df,method="pearson")
           ,layout="spring"
           ,label.cex=0.9
           ,labels=colnames(df)
           ,label.scale=F
           ,details=T
           ,edge.labels=F
           ,doNotPlot=T
           ,alpha=0.05
           ,minimum='sig'
           ,sampleSize=100)
    
    # calculating significance
    pvalMat <- Hmisc::rcorr(df,type="pearson")
    
    # loop to add text
    for(i in 1:(nrow(a$layout)-1)){
      for(j in (i+1):nrow(a$layout)){
    
        # is correlation statistically significant
        if(pvalMat$P[i,j] < 0.05){
          # using stored layout values, col1 is x, col2 is y
          loc_center_x <- (a$layout[i,1]+a$layout[j,1])/2
          loc_center_y <- (a$layout[i,2]+a$layout[j,2])/2
    
          # finding angle of vector
          rotation <- atan((a$layout[i,2]-a$layout[j,2])/(a$layout[i,1]-a$layout[j,1]))*180/pi
    
          # radial separation
          radius <- 0.1
    
          # putting text at location
          text(labels=round(cor(df,method="pearson")[i,j],digits=2) # text of correlation with rounded digits
               ,x=loc_center_x + abs(radius*sin(rotation*pi/180))
               ,y=loc_center_y + abs(radius*cos(rotation*pi/180))
               ,srt=rotation
               ,adj=c(0.5,0.5)
               ,cex=0.8)
    
        }
      }
    }
    

    【讨论】:

    • 很好的解决方案,UP UP UP
    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2023-03-22
    • 2019-06-19
    相关资源
    最近更新 更多