qgraph 可以在实际边缘之外渲染边缘标签吗?

Can qgraph render edge labels outside the actual edge?

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

似乎没有调整边缘标签跨轴位置的参数。一种解决方案是将边缘标签单独添加到图中。下面给出了一个例子,它产生了下面的图。一般的方法是获取绘图的布局,然后使用两个节点位置的平均值来沿线放置文本。对位置进行手动调整,以便文本通常与线平行,但大部分偏离线(基于角度的正弦和余弦的 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)

    }
  }
}