qgraph可以在实际边缘之外渲染边缘标签吗? [英] Can qgraph render edge labels outside the actual edge?

查看:196
本文介绍了qgraph可以在实际边缘之外渲染边缘标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了便于阅读,我试图在我的qgraph的实际边缘外插入边缘标签。我特别不喜欢在标签下方加入白色bg的选项,它将边缘拧紧。根据手册,只能沿着线条调整边缘标签位置,而不能在侧面调整边缘标签位置。有没有人与之前的斗争?是否有可能规避这个问题? Cheers

解决方案

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

  library(qgraph)

#数据
set.seed(10)
x1 < - rnorm(100,0,1)
x2 < - x1 + rnorm(100,0,0.2)
x3 < -x1 + x2 + rnorm(100,0,0.2)
x4 < - rorm(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)

#制作数据帧
df < - cbind(x1,x2,x3,x4,x5,x6,x7)

#计算相关矩阵的qgraph
#a存储布局
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)

#绘制实际图
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)

#计算重要性
pvalMat < - Hmisc :: rcorr(df,type =pearson )

#循环添加文本
for(i in 1:(nrow(a $ layout)-1)){
for(j in(i + 1) :nrow($布局)){

#是相关统计学显着的
if(pvalMat $ P [i,j]< 0.05){
#使用存储的布局值,col1是x,col2是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

#寻找矢量的角度
旋转< - atan ($ a $ layout [i,2] -a $ layout [j,2])/(a $ layout [i,1] -a $ layout [j,1]))* 180 / pi

#径向分离
半径<-0.1

#将文本放在位置
text(labels = round(cor(df,method =pearson)[i (半径* sin(旋转* pi / 180))
,y = loc_center_y + abs(半径* cos(rotation * pi / 180))
,srt =旋转
,adj = c(0.5,0.5)
,cex = 0.8)

}



I'm trying to insert edge labels outside the actual edge in my qgraph for readability purposes. I particularly don't like the option of include a white bg below the label, it screws up the edge. According to the manual, it is possible to adjust edge label position only along the line, but not on the side. Did anyone struggle with this before? Is it possible to circumvent this issue? Cheers

解决方案

There does not seem to be a parameter for adjusting the across axis location of the edge label. One solution is to add the edge labels to the plot separately. An example is given below, which yielded the following plot. The general method is to obtain the layout of the plot, and then use the average of the two node locations to place the text along the line. There is a manual adjustment to the position so that the text will generally be parallel to the line, but mostly off the line (x and y offsets based on sine and cosine of angle). If you want further control you could manually adjust some of the text() locations for better results.

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)

    }
  }
}

这篇关于qgraph可以在实际边缘之外渲染边缘标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆