Plotting group distances in R

心已入冬 提交于 2019-12-21 17:24:36

问题


I have a matrix of group distances G as follows

G <- data.frame( Gp1=c(6.525,15.915,16.605,10.665,19.345), Gp2=c(15.915,8.605,31.455,25.485,48.355), Gp3=c(16.605,31.455,7.955,11.195,33.685), Gp4=c(10.665,25.485,11.195,0,21.985), Gp5=c(19.345,48.355,33.685,21.985,0))
rownames(G) <- colnames(G)

G
       Gp1    Gp2    Gp3    Gp4    Gp5
Gp1  6.525 15.915 16.605 10.665 19.345
Gp2 15.915  8.605 31.455 25.485 48.355
Gp3 16.605 31.455  7.955 11.195 33.685
Gp4 10.665 25.485 11.195  0.000 21.985
Gp5 19.345 48.355 33.685 21.985  0.000

The diagonal is the within group distances.

I would like to depict the above in a plot as follows. It need not be to scale to the distance values. How to produce it in R?


回答1:


This comes pretty close:

[Note: Incorporated @Jealie's suggestion and made a few other changes. It's a bit closer to your intended result now.]

rownames(G) <- colnames(G) <- c("I","II","III","IV","V")
G[lower.tri(G)] <- 0
library(igraph)
g <- graph.adjacency(as.matrix(G),weight=T, mode="undirected")
g <- simplify(g,remove.loops=TRUE)
plot(g,edge.label=E(g)$weight, 
     vertex.label=paste(V(g)$name,diag(as.matrix(G)),sep="\n"),
     layout=layout.circle,
     vertex.size=30)

I'm not aware of a way to make the edge labels parallel to the edges, but that doesn't mean it cant' be done...




回答2:


Got the solution using qgraph

G <- data.frame( Gp1=c(6.525,15.915,16.605,10.665,19.345), Gp2=c(15.915,8.605,31.455,25.485,48.355), Gp3=c(16.605,31.455,7.955,11.195,33.685), Gp4=c(10.665,25.485,11.195,0,21.985), Gp5=c(19.345,48.355,33.685,21.985,0))
rownames(G) <- colnames(G)

rownames(G) <- colnames(G) <- as.character(as.roman(seq(length.out=nrow(G))))
qgraph(G, layout = "circle", usePCH = TRUE,
        normalise = TRUE,
        vsize = 7, color = "gray90", node.width = 1,
        border.width = 1.7, diag = FALSE,
        label.prop = 0.6,
        labels = paste(rownames(G),diag(as.matrix(G)),sep="\n"),
        esize = 1, edge.labels = TRUE, edge.color = "black", fade=FALSE,
        lty = "dotted", edge.label.cex = 0.7, edge.label.bg = "white",
        directed = TRUE, arrows = FALSE, bidirectional = TRUE,
        asize = 1.5, weighted = FALSE)



来源:https://stackoverflow.com/questions/24222267/plotting-group-distances-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!