3

読みやすくするために、qgraph の実際のエッジの外側にエッジ ラベルを挿入しようとしています。私は特にラベルの下に白い背景を含めるオプションが好きではありません。それは端を台無しにします。マニュアルによると、エッジラベルの位置は線に沿ってのみ調整でき、側面では調整できないとのことです。以前に誰かがこれに苦労しましたか?この問題を回避することは可能ですか? 乾杯

4

1 に答える 1

3

エッジ ラベルの横軸の位置を調整するためのパラメーターはないようです。1 つの解決策は、エッジ ラベルを個別にプロットに追加することです。以下に例を示します。これにより、次のプロットが生成されます。一般的な方法は、プロットのレイアウトを取得してから、2 つのノード位置の平均を使用して線に沿ってテキストを配置することです。テキストが通常は線と平行になるように位置を手動で調整しますが、ほとんどは線から外れます (角度のサインとコサインに基づく 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)

    }
  }
}
于 2016-02-28T18:53:38.407 に答える