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

时间:2016-02-23 11:27:19

标签: r graph label readability r-qgraph

为了便于阅读,我试图在qgraph的实际边缘之外插入边缘标签。我特别不喜欢在标签下面加一个白色的bg,它会把边缘搞得一团糟。根据手册,可以仅沿着线调整边缘标签位置,而不是侧面。以前有没有人为此而斗争?有可能绕过这个问题吗?干杯

1 个答案:

答案 0 :(得分:2)

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

enter image description here

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)

    }
  }
}