绘制R中的组距离

时间:2014-06-14 17:02:57

标签: r plot ggplot2 visualization

我有一组距离G,如下所示

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

对角线是组内距离。

我想在如下的情节中描绘上述内容。不必缩放到距离值。如何在R中制作它?

enter image description here

2 个答案:

答案 0 :(得分:6)

这非常接近:

[注:遵守@ Jealie的建议并做了一些其他修改。现在它更接近您的预期结果。]

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)

我不知道如何使边缘标签与边缘平行,但这并不意味着它无法完成......

答案 1 :(得分:1)

使用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)

enter image description here