2016-04-16 16 views
1

создать дендрограммы используя ggdendro таким образом:Добавьте ярлыки дендрограммы и цвет кластеров

library(igraph) 
library(RColorBrewer) 
library(GGally) 
library(ggplot2) 
library(plotrix) 
library(extrafont) 
library(ggdendro) 

# load dataset 
net <- read.graph("./dataset/lesmiserables.gml", format = c("gml")) 
deg <- igraph::degree(net, mode = "all") 

# find communities 
girvNew <- cluster_edge_betweenness(net) 

girvNew_sizesComm <- sizes(girvNew) 
girvNew_numComm <- length(girvNew_sizesComm) 

# colors 
colorsRainbow <- rainbow(max(membership(girvNew)), alpha = 0.6) 

x11() 
plot(net, 
    vertex.size = plotrix::rescale(deg, c(5, 16)), 
    vertex.color = colorsRainbow[membership(girvNew)], 
    vertex.frame.color = NA, 
    vertex.label = NA, 
    vertex.size = 10, 
    edge.color = "#d8d8d8", 
    layout = layout.fruchterman.reingold, 
    main = "Detected communities") 

# find dendrogram 
girvNew_den <- as.dendrogram(girvNew) 

#convert cluster object to use with ggplot 
girvNew_dendrogram <- dendro_data(girvNew_den, type = "rectangle") 

x11() 
ggdendrogram(girvNew_dendrogram, 
      rotate = TRUE, 
      labels = TRUE, 
      segments = TRUE, 
      leaf_labels = TRUE, 
      theme_dendro = FALSE) 

И я получаю: enter image description here enter image description here

Моя сеть:

> dput(net) 
structure(list(77, FALSE, c(1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 11, 
11, 11, 11, 12, 13, 14, 15, 17, 18, 18, 19, 19, 19, 20, 20, 20, 
20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 
23, 23, 23, 23, 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 
27, 27, 27, 28, 28, 29, 29, 29, 30, 31, 31, 31, 31, 32, 33, 33, 
34, 34, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 
38, 38, 38, 38, 39, 40, 41, 41, 42, 42, 42, 43, 43, 43, 44, 44, 
45, 47, 48, 48, 48, 48, 49, 49, 50, 50, 51, 51, 51, 52, 52, 53, 
54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 57, 
57, 57, 58, 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 61, 61, 
61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 
63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 
65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66, 
66, 67, 68, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 70, 
70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 71, 72, 
72, 72, 73, 74, 74, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76, 
76, 76, 76), c(0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 10, 3, 2, 0, 11, 
11, 11, 11, 16, 16, 17, 16, 17, 18, 16, 17, 18, 19, 16, 17, 18, 
19, 20, 16, 17, 18, 19, 20, 21, 16, 17, 18, 19, 20, 21, 22, 12, 
11, 23, 11, 24, 23, 11, 24, 11, 16, 25, 11, 23, 25, 24, 26, 11, 
27, 23, 27, 11, 23, 30, 11, 23, 27, 11, 11, 27, 11, 29, 11, 34, 
29, 34, 35, 11, 29, 34, 35, 36, 11, 29, 34, 35, 36, 37, 11, 29, 
25, 25, 24, 25, 41, 25, 24, 11, 26, 27, 28, 11, 28, 46, 47, 25, 
27, 11, 26, 11, 49, 24, 49, 26, 11, 51, 39, 51, 51, 49, 26, 51, 
49, 39, 54, 26, 11, 16, 25, 41, 48, 49, 55, 55, 41, 48, 55, 48, 
27, 57, 11, 58, 55, 48, 57, 48, 58, 59, 48, 58, 60, 59, 57, 55, 
55, 58, 59, 48, 57, 41, 61, 60, 59, 48, 62, 57, 58, 61, 60, 55, 
55, 62, 48, 63, 58, 61, 60, 59, 57, 11, 63, 64, 48, 62, 58, 61, 
60, 59, 57, 55, 64, 58, 59, 62, 65, 48, 63, 61, 60, 57, 25, 11, 
24, 27, 48, 41, 25, 68, 11, 24, 27, 48, 41, 25, 69, 68, 11, 24, 
27, 41, 58, 27, 69, 68, 70, 11, 48, 41, 25, 26, 27, 11, 48, 48, 
73, 69, 68, 25, 48, 41, 70, 71, 64, 65, 66, 63, 62, 48, 58), 
    c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 12, 11, 10, 14, 15, 16, 
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
    32, 33, 34, 35, 36, 37, 38, 47, 46, 39, 40, 41, 42, 43, 44, 
    45, 49, 48, 52, 51, 50, 54, 55, 53, 56, 57, 58, 60, 59, 61, 
    62, 63, 66, 64, 65, 67, 69, 70, 71, 68, 72, 73, 74, 75, 76, 
    77, 79, 78, 82, 83, 80, 81, 87, 88, 84, 85, 86, 93, 94, 89, 
    90, 91, 92, 95, 96, 97, 98, 101, 100, 99, 102, 103, 104, 
    106, 105, 107, 108, 112, 110, 111, 109, 114, 113, 116, 115, 
    119, 118, 117, 121, 120, 122, 125, 124, 123, 131, 132, 133, 
    130, 128, 134, 135, 127, 126, 129, 136, 137, 139, 140, 138, 
    145, 143, 142, 141, 144, 148, 147, 149, 146, 150, 151, 152, 
    153, 158, 157, 154, 156, 155, 164, 162, 159, 163, 160, 161, 
    166, 165, 168, 174, 170, 171, 167, 173, 172, 169, 184, 177, 
    175, 183, 179, 182, 181, 180, 176, 178, 187, 194, 193, 189, 
    192, 191, 190, 188, 185, 186, 200, 196, 197, 203, 202, 198, 
    201, 195, 199, 204, 206, 207, 205, 208, 210, 209, 213, 214, 
    211, 215, 217, 216, 212, 221, 222, 218, 223, 224, 225, 220, 
    219, 230, 233, 226, 232, 231, 228, 227, 229, 236, 234, 235, 
    237, 238, 239, 242, 244, 243, 241, 240, 245, 246, 252, 253, 
    251, 250, 247, 248, 249), c(0, 1, 2, 4, 5, 6, 7, 8, 9, 13, 
    3, 12, 11, 10, 14, 15, 16, 17, 47, 49, 52, 54, 57, 62, 66, 
    69, 72, 73, 75, 77, 82, 87, 93, 102, 106, 112, 114, 119, 
    131, 145, 184, 206, 213, 221, 230, 236, 46, 18, 19, 21, 24, 
    28, 33, 39, 55, 132, 20, 22, 25, 29, 34, 40, 23, 26, 30, 
    35, 41, 27, 31, 36, 42, 32, 37, 43, 38, 44, 45, 48, 51, 58, 
    64, 67, 70, 50, 53, 60, 97, 101, 116, 207, 214, 222, 56, 
    59, 95, 96, 98, 100, 110, 133, 205, 211, 218, 233, 242, 61, 
    103, 113, 118, 125, 130, 234, 63, 65, 71, 74, 104, 111, 143, 
    208, 215, 223, 226, 235, 105, 107, 76, 79, 83, 88, 94, 68, 
    78, 80, 84, 89, 81, 85, 90, 86, 91, 92, 121, 128, 99, 134, 
    139, 164, 210, 217, 224, 232, 244, 108, 109, 135, 140, 142, 
    148, 150, 153, 162, 168, 177, 187, 200, 209, 216, 231, 237, 
    238, 243, 252, 115, 117, 124, 127, 136, 120, 122, 123, 126, 
    129, 137, 138, 141, 147, 158, 159, 174, 175, 194, 144, 149, 
    157, 163, 170, 183, 193, 204, 146, 151, 154, 160, 171, 179, 
    189, 196, 225, 253, 152, 156, 161, 167, 182, 192, 197, 155, 
    166, 173, 181, 191, 203, 165, 172, 180, 190, 202, 169, 176, 
    188, 198, 251, 178, 185, 201, 250, 186, 195, 247, 199, 248, 
    249, 212, 220, 228, 241, 219, 227, 240, 229, 245, 246, 239 
    ), c(0, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 10, 14, 15, 16, 17, 
    18, 18, 19, 21, 24, 28, 33, 39, 48, 50, 53, 57, 62, 64, 67, 
    68, 72, 73, 75, 77, 80, 84, 89, 95, 96, 97, 99, 102, 105, 
    107, 108, 108, 109, 113, 115, 117, 120, 122, 123, 126, 136, 
    138, 141, 146, 150, 153, 159, 167, 175, 185, 195, 204, 205, 
    211, 218, 226, 234, 237, 238, 240, 247, 254), c(0, 10, 10, 
    12, 13, 13, 13, 13, 13, 13, 13, 14, 46, 47, 47, 47, 47, 56, 
    62, 67, 71, 74, 76, 77, 83, 92, 105, 112, 124, 126, 131, 
    132, 132, 132, 132, 136, 139, 141, 142, 142, 144, 144, 153, 
    153, 153, 153, 153, 154, 155, 173, 178, 178, 182, 182, 182, 
    183, 192, 192, 200, 210, 217, 223, 228, 233, 237, 240, 242, 
    243, 243, 247, 250, 252, 253, 253, 254, 254, 254, 254), list(
     c(1, 0, 1), structure(list(), .Names = character(0)), 
     structure(list(id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
     11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
     39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 
     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 
     67, 68, 69, 70, 71, 72, 73, 74, 75, 76), label = c("Myriel", 
     "Napoleon", "MlleBaptistine", "MmeMagloire", "CountessDeLo", 
     "Geborand", "Champtercier", "Cravatte", "Count", "OldMan", 
     "Labarre", "Valjean", "Marguerite", "MmeDeR", "Isabeau", 
     "Gervais", "Tholomyes", "Listolier", "Fameuil", "Blacheville", 
     "Favourite", "Dahlia", "Zephine", "Fantine", "MmeThenardier", 
     "Thenardier", "Cosette", "Javert", "Fauchelevent", "Bamatabois", 
     "Perpetue", "Simplice", "Scaufflaire", "Woman1", "Judge", 
     "Champmathieu", "Brevet", "Chenildieu", "Cochepaille", 
     "Pontmercy", "Boulatruelle", "Eponine", "Anzelma", "Woman2", 
     "MotherInnocent", "Gribier", "Jondrette", "MmeBurgon", 
     "Gavroche", "Gillenormand", "Magnon", "MlleGillenormand", 
     "MmePontmercy", "MlleVaubois", "LtGillenormand", "Marius", 
     "BaronessT", "Mabeuf", "Enjolras", "Combeferre", "Prouvaire", 
     "Feuilly", "Courfeyrac", "Bahorel", "Bossuet", "Joly", 
     "Grantaire", "MotherPlutarch", "Gueulemer", "Babet", 
     "Claquesous", "Montparnasse", "Toussaint", "Child1", 
     "Child2", "Brujon", "MmeHucheloup"), maincharacter = c(0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0)), .Names = c("id", "label", "maincharacter" 
     )), structure(list(value = c(1, 8, 10, 6, 1, 1, 1, 1, 
     2, 1, 1, 3, 3, 5, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 3, 3, 
     3, 4, 3, 3, 3, 3, 5, 3, 3, 3, 3, 4, 4, 3, 3, 3, 3, 4, 
     4, 4, 2, 9, 2, 7, 13, 1, 12, 4, 31, 1, 1, 17, 5, 5, 1, 
     1, 8, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 2, 1, 3, 2, 3, 3, 
     2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 
     2, 3, 2, 2, 1, 3, 1, 1, 3, 1, 2, 1, 2, 1, 1, 1, 3, 2, 
     1, 1, 9, 2, 2, 1, 1, 1, 2, 1, 1, 6, 12, 1, 1, 21, 19, 
     1, 2, 5, 4, 1, 1, 1, 1, 1, 7, 7, 6, 1, 4, 15, 5, 6, 2, 
     1, 4, 2, 2, 6, 2, 5, 1, 1, 9, 17, 13, 7, 2, 1, 6, 3, 
     5, 5, 6, 2, 4, 3, 2, 1, 5, 12, 5, 4, 10, 6, 2, 9, 1, 
     1, 5, 7, 3, 5, 5, 5, 2, 5, 1, 2, 3, 3, 1, 2, 2, 1, 1, 
     1, 1, 3, 5, 1, 1, 1, 1, 1, 6, 6, 1, 1, 2, 1, 1, 4, 4, 
     4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 
     2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "value")), 
    <environment>), class = "igraph") 

Желательно, чтобы на вертикальной оси были имена сетевых узлов, а линии кластера одинаковы цвет сообществ, рассчитанный с помощью функции cluster_edge_betweenness.

Это действительно не имеет значения, если они красочны:

  • узлы (ярлыки) или
  • линия кластеров или
  • фон каждый кластера.

Важно то, что я могу различать кластеры, основанные на цветах, как я могу сделать на первом участке (с круглыми узлами).

Как я могу это сделать? Я искал в Интернете, но я не мог решить. Спасибо


Я пытался следовать оба этих двух дорог:

  1. после ответа jlhoward в this post
  2. использования dendextend package

У меня были проблемы с самого начала для и то и другое.

(1) В ответ df это кадр данных, в то время как у меня есть список (net), и я не знаю, как правильно преобразовать свою сеть в кадре данных, как в примере , Во всяком случае, если я стараюсь (только играть), чтобы сделать rownames(df) <- V(net)$label я

Ошибка в row.names<-.data.frame (*tmp*, = значение): длина 'row.names' неприемлема

Совершенно верно. Но график результат: enter image description here

Это не использовать ярлык узлов сети net но те df сети. Мне кажется очень странным.

(2) Это то, что я сделал:

library(igraph) 
library(GGally) 
library(ggplot2) 
library(ggdendro) 
library(dendextend) 
library(dendextendRcpp) 
library(zoo) 

# load dataset 
net <- read.graph("./dataset/lesmiserables.gml", format = c("gml")) 
deg <- igraph::degree(net, mode = "all") 

comm <- fastgreedy.community(net) 

comm_sizesComm <- sizes(comm) 
comm_numComm <- length(comm_sizesComm) 
comm_modularity <- modularity(comm) 

# plot communities 
colorsRainbow <- rainbow(max(membership(comm)), alpha = 0.6) 
pdf(file = "./output/prova_comm2.pdf") 
plot(net, 
    vertex.size = plotrix::rescale(deg, c(5, 16)), 
    vertex.color = colorsRainbow[membership(comm)], 
    vertex.frame.color = NA, 
    vertex.label.color = "black", 
    vertex.label.cex = 0.5, 
    vertex.size = 10, 
    edge.color = "#d8d8d8", 
    layout = layout.fruchterman.reingold, 
    edge.curved = FALSE) 
dev.off() 

# create dendrogram 
dend <- as.dendrogram(comm) 

cut <- comm_numComm 

# change labels 
dend <- dend %>% set("labels", V(net)$label) # change label 
dend <- dend %>% set("labels_col", "black") # change color 
dend <- dend %>% set("labels_cex", .5) # change size 

# color label based on communities 
colorsRainbow <- rainbow(max(membership(comm)), alpha = 1) 
dend <- dend %>% set("labels_col", value = colorsRainbow, k = cut) 

# plot dendrogram 
pdf(file = "./output/prova_dend2.pdf") 
plot(dend, 
    horiz = TRUE, 
    main = "Dendrogram") 
dev.off() 

Я succed изменить метку и цвет их. Но цвета не совпадают между двумя графиками. Я не понимаю, является ли это проблемой сюжета или построения дендограммы.

Объясняет: В первом участке (один с узлами, представленных точками) я, что в синем сообществе есть узлы {Perpetual, Fantine, Anzelma, Simplice, ...}. В желтой общине есть {Woman2, Marius, Magnon, Cosette, ...}.

Если теперь я иду, чтобы увидеть второй участок (дендрограммы), я вижу, что Woman2 и Anzelma узлы находятся в двух разных сообществах. Кажется, огромная проблема, и я не знаю, как начать решать.

(3) Я попытался также:

ggd1 <- as.ggdend(dend) 
ggplot(ggd1) 

И проблема такая же, из (2): общины не совпадают.


Я создаю этот простой график:

> dput(net) 
structure(list(16, FALSE, c(1, 2, 3, 2, 3, 3, 4, 5, 5, 7, 7, 
9, 8, 10, 9, 11, 11, 14, 15, 15, 15, 12, 14), c(0, 0, 0, 1, 2, 
1, 3, 3, 4, 3, 6, 6, 7, 8, 8, 8, 10, 11, 14, 13, 12, 3, 1), c(0, 
1, 3, 2, 5, 4, 6, 7, 8, 9, 10, 12, 11, 14, 13, 15, 16, 21, 22, 
17, 20, 19, 18), c(0, 1, 2, 3, 5, 22, 4, 6, 7, 9, 21, 8, 10, 
11, 12, 14, 13, 15, 16, 17, 20, 19, 18), c(0, 0, 1, 3, 6, 7, 
9, 9, 11, 12, 14, 15, 17, 18, 18, 20, 23), c(0, 3, 6, 7, 11, 
12, 12, 14, 15, 18, 18, 19, 20, 21, 22, 23, 23), list(c(1, 0, 
1), structure(list(), .Names = character(0)), structure(list(
    id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
    15), label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", 
    "L", "M", "N", "O", "P", "Q", "R")), .Names = c("id", "label" 
)), list()), <environment>), class = "igraph") 

enter image description here

, а затем я нахожу общины и дендрограммы:

# calculate degree 
deg <- igraph::degree(net, mode = "all") 

# find communities 
com <- cluster_edge_betweenness(net) 
## or use 
# com <- fastgreedy.community(net) 

# details on communities 
com_sizesCom <- sizes(com) 
com_numCom <- length(com_sizesCom) 

# create dendrogram 
dend <- as.dendrogram(com) 
labels(dend) <- V(net)$label[order.dendrogram(dend)] 

k <- max(membership(com)) 
colorsRainbow <- rainbow(k, alpha = 1) 

set.seed(23420) 
colorsRainbow <- rainbow(max(membership(com))) 
colorsRainbow <- sample(colorsRainbow) 
dend <- dend %>% set("labels_col", value = colorsRainbow, k = k) 
dend <- dend %>% set("branches_k_color", value = colorsRainbow, k = k) 
dend <- dend %>% set("labels_cex", 1) 

я получаю: enter image description here enter image description here

Итак, цвета неправильные.


Весь обновленный код. Теперь синий и зеленый переключаются.

library(igraph) 
library(dendextend) 
library(colorspace) 

net <- upgrade_graph(net) 
# dput(net) 

# calculate degree 
deg <- igraph::degree(net, mode = "all") 

# plot network 
x11() 
plot(net, 
     vertex.size = plotrix::rescale(deg, c(8, 22)), 
     vertex.color = "tomato", 
     vertex.frame.color = NA, 
     vertex.label.color = "black", 
     vertex.size = 10, 
     edge.color = "#d8d8d8", 
     layout = layout.fruchterman.reingold, 
     edge.curved = FALSE) 

# find communities 
com <- cluster_edge_betweenness(net) 

# details on communities 
com_sizesCom <- sizes(com) 
com_numCom <- length(com_sizesCom) 
print(com_numCom) 

# plot communities 
colorsRainbow <- rainbow(max(membership(com)), alpha = 0.6) 
x11() 
par(mar = c(1, 1, 1, 1)) 
plot(net, 
     vertex.size = plotrix::rescale(deg, c(8, 22)), 
     vertex.color = colorsRainbow[membership(com)], 
     vertex.frame.color = NA, 
     edge.color = "#d8d8d8", 
     vertex.label.color = "black", 
     layout = layout.fruchterman.reingold, 
     edge.curved = FALSE) 

# create dendrogram 
dend <- as.dendrogram(com) 
labels(dend) <- V(net)$label[order.dendrogram(dend)] 

k <- max(membership(com)) 
colorsRainbow <- rainbow(k, alpha = 1) 

colorsRainbow <- rev(unique(colorsRainbow[membership(com)[order.dendrogram(dend)]])) 
dend <- dend %>% set("labels_col", value = colorsRainbow, k = k) 
dend <- dend %>% set("branches_k_color", value = colorsRainbow, k = k) 
dend <- dend %>% set("labels_cex", 1) 

# plot dendrogram 
x11() 
par(mar = c(3, 1, 1, 5)) 
plot(dend, horiz = T) 
+2

Вы видели [пакет dendextend] (https://cran.r-project.org/web/packages/dendextend/ виньетки/introduction.html). Кроме того, некоторые цветные дендрограммы [здесь] (http://stackoverflow.com/questions/21474388/colorize-clusters-in-dendogram-with-ggplot2) –

+0

Привет @marielle, вы не можете (легко) добавить цвета с помощью ggdendro.Это была мотивация для объекта ggdend в пакете dendextend. –

+0

@SandyMuspratt Спасибо, я редактирую главное сообщение, надеюсь, вы можете мне помочь. – marielle

ответ

0

Я не уверен, что я получил этикетку заказ правильно (так как я не знаю, как они извлекаются из сети), но вот попытка из воспроизводимых например:

library(igraph) 

net <- structure(list(77, FALSE, c(1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 11, 
11, 11, 11, 12, 13, 14, 15, 17, 18, 18, 19, 19, 19, 20, 20, 20, 
20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 
23, 23, 23, 23, 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 
27, 27, 27, 28, 28, 29, 29, 29, 30, 31, 31, 31, 31, 32, 33, 33, 
34, 34, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 
38, 38, 38, 38, 39, 40, 41, 41, 42, 42, 42, 43, 43, 43, 44, 44, 
45, 47, 48, 48, 48, 48, 49, 49, 50, 50, 51, 51, 51, 52, 52, 53, 
54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 57, 
57, 57, 58, 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 61, 61, 
61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 
63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 
65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66, 
66, 67, 68, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 70, 
70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 71, 72, 
72, 72, 73, 74, 74, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76, 
76, 76, 76), c(0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 10, 3, 2, 0, 11, 
11, 11, 11, 16, 16, 17, 16, 17, 18, 16, 17, 18, 19, 16, 17, 18, 
19, 20, 16, 17, 18, 19, 20, 21, 16, 17, 18, 19, 20, 21, 22, 12, 
11, 23, 11, 24, 23, 11, 24, 11, 16, 25, 11, 23, 25, 24, 26, 11, 
27, 23, 27, 11, 23, 30, 11, 23, 27, 11, 11, 27, 11, 29, 11, 34, 
29, 34, 35, 11, 29, 34, 35, 36, 11, 29, 34, 35, 36, 37, 11, 29, 
25, 25, 24, 25, 41, 25, 24, 11, 26, 27, 28, 11, 28, 46, 47, 25, 
27, 11, 26, 11, 49, 24, 49, 26, 11, 51, 39, 51, 51, 49, 26, 51, 
49, 39, 54, 26, 11, 16, 25, 41, 48, 49, 55, 55, 41, 48, 55, 48, 
27, 57, 11, 58, 55, 48, 57, 48, 58, 59, 48, 58, 60, 59, 57, 55, 
55, 58, 59, 48, 57, 41, 61, 60, 59, 48, 62, 57, 58, 61, 60, 55, 
55, 62, 48, 63, 58, 61, 60, 59, 57, 11, 63, 64, 48, 62, 58, 61, 
60, 59, 57, 55, 64, 58, 59, 62, 65, 48, 63, 61, 60, 57, 25, 11, 
24, 27, 48, 41, 25, 68, 11, 24, 27, 48, 41, 25, 69, 68, 11, 24, 
27, 41, 58, 27, 69, 68, 70, 11, 48, 41, 25, 26, 27, 11, 48, 48, 
73, 69, 68, 25, 48, 41, 70, 71, 64, 65, 66, 63, 62, 48, 58), 
    c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 12, 11, 10, 14, 15, 16, 
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
    32, 33, 34, 35, 36, 37, 38, 47, 46, 39, 40, 41, 42, 43, 44, 
    45, 49, 48, 52, 51, 50, 54, 55, 53, 56, 57, 58, 60, 59, 61, 
    62, 63, 66, 64, 65, 67, 69, 70, 71, 68, 72, 73, 74, 75, 76, 
    77, 79, 78, 82, 83, 80, 81, 87, 88, 84, 85, 86, 93, 94, 89, 
    90, 91, 92, 95, 96, 97, 98, 101, 100, 99, 102, 103, 104, 
    106, 105, 107, 108, 112, 110, 111, 109, 114, 113, 116, 115, 
    119, 118, 117, 121, 120, 122, 125, 124, 123, 131, 132, 133, 
    130, 128, 134, 135, 127, 126, 129, 136, 137, 139, 140, 138, 
    145, 143, 142, 141, 144, 148, 147, 149, 146, 150, 151, 152, 
    153, 158, 157, 154, 156, 155, 164, 162, 159, 163, 160, 161, 
    166, 165, 168, 174, 170, 171, 167, 173, 172, 169, 184, 177, 
    175, 183, 179, 182, 181, 180, 176, 178, 187, 194, 193, 189, 
    192, 191, 190, 188, 185, 186, 200, 196, 197, 203, 202, 198, 
    201, 195, 199, 204, 206, 207, 205, 208, 210, 209, 213, 214, 
    211, 215, 217, 216, 212, 221, 222, 218, 223, 224, 225, 220, 
    219, 230, 233, 226, 232, 231, 228, 227, 229, 236, 234, 235, 
    237, 238, 239, 242, 244, 243, 241, 240, 245, 246, 252, 253, 
    251, 250, 247, 248, 249), c(0, 1, 2, 4, 5, 6, 7, 8, 9, 13, 
    3, 12, 11, 10, 14, 15, 16, 17, 47, 49, 52, 54, 57, 62, 66, 
    69, 72, 73, 75, 77, 82, 87, 93, 102, 106, 112, 114, 119, 
    131, 145, 184, 206, 213, 221, 230, 236, 46, 18, 19, 21, 24, 
    28, 33, 39, 55, 132, 20, 22, 25, 29, 34, 40, 23, 26, 30, 
    35, 41, 27, 31, 36, 42, 32, 37, 43, 38, 44, 45, 48, 51, 58, 
    64, 67, 70, 50, 53, 60, 97, 101, 116, 207, 214, 222, 56, 
    59, 95, 96, 98, 100, 110, 133, 205, 211, 218, 233, 242, 61, 
    103, 113, 118, 125, 130, 234, 63, 65, 71, 74, 104, 111, 143, 
    208, 215, 223, 226, 235, 105, 107, 76, 79, 83, 88, 94, 68, 
    78, 80, 84, 89, 81, 85, 90, 86, 91, 92, 121, 128, 99, 134, 
    139, 164, 210, 217, 224, 232, 244, 108, 109, 135, 140, 142, 
    148, 150, 153, 162, 168, 177, 187, 200, 209, 216, 231, 237, 
    238, 243, 252, 115, 117, 124, 127, 136, 120, 122, 123, 126, 
    129, 137, 138, 141, 147, 158, 159, 174, 175, 194, 144, 149, 
    157, 163, 170, 183, 193, 204, 146, 151, 154, 160, 171, 179, 
    189, 196, 225, 253, 152, 156, 161, 167, 182, 192, 197, 155, 
    166, 173, 181, 191, 203, 165, 172, 180, 190, 202, 169, 176, 
    188, 198, 251, 178, 185, 201, 250, 186, 195, 247, 199, 248, 
    249, 212, 220, 228, 241, 219, 227, 240, 229, 245, 246, 239 
    ), c(0, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 10, 14, 15, 16, 17, 
    18, 18, 19, 21, 24, 28, 33, 39, 48, 50, 53, 57, 62, 64, 67, 
    68, 72, 73, 75, 77, 80, 84, 89, 95, 96, 97, 99, 102, 105, 
    107, 108, 108, 109, 113, 115, 117, 120, 122, 123, 126, 136, 
    138, 141, 146, 150, 153, 159, 167, 175, 185, 195, 204, 205, 
    211, 218, 226, 234, 237, 238, 240, 247, 254), c(0, 10, 10, 
    12, 13, 13, 13, 13, 13, 13, 13, 14, 46, 47, 47, 47, 47, 56, 
    62, 67, 71, 74, 76, 77, 83, 92, 105, 112, 124, 126, 131, 
    132, 132, 132, 132, 136, 139, 141, 142, 142, 144, 144, 153, 
    153, 153, 153, 153, 154, 155, 173, 178, 178, 182, 182, 182, 
    183, 192, 192, 200, 210, 217, 223, 228, 233, 237, 240, 242, 
    243, 243, 247, 250, 252, 253, 253, 254, 254, 254, 254), list(
     c(1, 0, 1), structure(list(), .Names = character(0)), 
     structure(list(id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
     11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
     39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 
     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 
     67, 68, 69, 70, 71, 72, 73, 74, 75, 76), label = c("Myriel", 
     "Napoleon", "MlleBaptistine", "MmeMagloire", "CountessDeLo", 
     "Geborand", "Champtercier", "Cravatte", "Count", "OldMan", 
     "Labarre", "Valjean", "Marguerite", "MmeDeR", "Isabeau", 
     "Gervais", "Tholomyes", "Listolier", "Fameuil", "Blacheville", 
     "Favourite", "Dahlia", "Zephine", "Fantine", "MmeThenardier", 
     "Thenardier", "Cosette", "Javert", "Fauchelevent", "Bamatabois", 
     "Perpetue", "Simplice", "Scaufflaire", "Woman1", "Judge", 
     "Champmathieu", "Brevet", "Chenildieu", "Cochepaille", 
     "Pontmercy", "Boulatruelle", "Eponine", "Anzelma", "Woman2", 
     "MotherInnocent", "Gribier", "Jondrette", "MmeBurgon", 
     "Gavroche", "Gillenormand", "Magnon", "MlleGillenormand", 
     "MmePontmercy", "MlleVaubois", "LtGillenormand", "Marius", 
     "BaronessT", "Mabeuf", "Enjolras", "Combeferre", "Prouvaire", 
     "Feuilly", "Courfeyrac", "Bahorel", "Bossuet", "Joly", 
     "Grantaire", "MotherPlutarch", "Gueulemer", "Babet", 
     "Claquesous", "Montparnasse", "Toussaint", "Child1", 
     "Child2", "Brujon", "MmeHucheloup"), maincharacter = c(0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0)), .Names = c("id", "label", "maincharacter" 
     )), structure(list(value = c(1, 8, 10, 6, 1, 1, 1, 1, 
     2, 1, 1, 3, 3, 5, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 3, 3, 
     3, 4, 3, 3, 3, 3, 5, 3, 3, 3, 3, 4, 4, 3, 3, 3, 3, 4, 
     4, 4, 2, 9, 2, 7, 13, 1, 12, 4, 31, 1, 1, 17, 5, 5, 1, 
     1, 8, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 2, 1, 3, 2, 3, 3, 
     2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 
     2, 3, 2, 2, 1, 3, 1, 1, 3, 1, 2, 1, 2, 1, 1, 1, 3, 2, 
     1, 1, 9, 2, 2, 1, 1, 1, 2, 1, 1, 6, 12, 1, 1, 21, 19, 
     1, 2, 5, 4, 1, 1, 1, 1, 1, 7, 7, 6, 1, 4, 15, 5, 6, 2, 
     1, 4, 2, 2, 6, 2, 5, 1, 1, 9, 17, 13, 7, 2, 1, 6, 3, 
     5, 5, 6, 2, 4, 3, 2, 1, 5, 12, 5, 4, 10, 6, 2, 9, 1, 
     1, 5, 7, 3, 5, 5, 5, 2, 5, 1, 2, 3, 3, 1, 2, 2, 1, 1, 
     1, 1, 3, 5, 1, 1, 1, 1, 1, 6, 6, 1, 1, 2, 1, 1, 4, 4, 
     4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 
     2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "value")), 
    "<environment>"), class = "igraph") 

girvNew <- cluster_edge_betweenness(upgrade_graph(net)) 
dend <- as.dendrogram(girvNew) 

library(dendextend) 
# hopefully this is correct, but I'm not sure. It assumes that 
# V(net)$label has the order of the original data 
labels(dend) <- V(net)$label[order.dendrogram(dend)] 

plot(dend) 

library(colorspace) 
k <- max(membership(girvNew)) 
colorsRainbow <- rainbow_hcl(k, alpha = 1) # [membership(girvNew)] 

set.seed(23420) 
colorsRainbow <- rainbow_hcl(max(membership(girvNew))) 
colorsRainbow <- sample(colorsRainbow) 
dend <- dend %>% set("labels_col", value = colorsRainbow, k = k) 
dend <- dend %>% set("branches_k_color", value = colorsRainbow, k = k) 

par(mar = c(3,1,1,5)) 
plot(dend, horiz = T) 

Выхода :

enter image description here

И теперь с ggplot2:

library(ggplot2) 
ggplot(dend) # the same as: ggplot(as.ggdend(dend)) 
# more work is needed for fixing the margins etc. 

enter image description here

Обновленный код:

# dput(net) 
net <- structure(list(16, FALSE, c(1, 2, 3, 2, 3, 3, 4, 5, 5, 7, 7, 
9, 8, 10, 9, 11, 11, 14, 15, 15, 15, 12, 14), c(0, 0, 0, 1, 2, 
1, 3, 3, 4, 3, 6, 6, 7, 8, 8, 8, 10, 11, 14, 13, 12, 3, 1), c(0, 
1, 3, 2, 5, 4, 6, 7, 8, 9, 10, 12, 11, 14, 13, 15, 16, 21, 22, 
17, 20, 19, 18), c(0, 1, 2, 3, 5, 22, 4, 6, 7, 9, 21, 8, 10, 
11, 12, 14, 13, 15, 16, 17, 20, 19, 18), c(0, 0, 1, 3, 6, 7, 
9, 9, 11, 12, 14, 15, 17, 18, 18, 20, 23), c(0, 3, 6, 7, 11, 
12, 12, 14, 15, 18, 18, 19, 20, 21, 22, 23, 23), list(c(1, 0, 
1), structure(list(), .Names = character(0)), structure(list(
    id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
    15), label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", 
    "L", "M", "N", "O", "P", "Q", "R")), .Names = c("id", "label" 
)), list()), "<environment>"), class = "igraph") 


library(igraph) 
net <- upgrade_graph(net) 
# calculate degree 
deg <- degree(net, mode = "all") 

# find communities 
com <- cluster_edge_betweenness(net) 
## or use 
# com <- fastgreedy.community(net) 

# details on communities 
com_sizesCom <- sizes(com) 
com_numCom <- length(com_sizesCom) 


# create dendrogram 
dend <- as.dendrogram(com) 
plot(dend) 
library(dendextend) 
labels(dend) <- V(net)$label[order.dendrogram(dend)] 
plot(dend) 

k <- max(membership(com)) 
colorsRainbow <- rainbow(k, alpha = 1) 

# colorsRainbow <- sample(colorsRainbow) 
# dend <- rotate(dend, order(membership(comm))) 
colorsRainbow <- rev(unique(colorsRainbow[membership(comm)[order.dendrogram(dend)]])) 
dend <- dend %>% set("labels_col", value = colorsRainbow, k = k) 
dend <- dend %>% set("branches_k_col", value = colorsRainbow, k = k) 
dend <- dend %>% set("labels_cex", 1) 
plot(dend) 

enter image description here

+0

Я проверил результаты, и он не работает правильно. Например, график (с точками) показывает, что у красного сообщества 10 членов, а в дендрограмме у красного сообщества всего 3 члена. – marielle

+0

Создайте гораздо меньшую сеть, сделайте воспроизводимый код сверху вниз - с этим я мог бы гораздо легче устранить его. –

+0

Спасибо за ваше время. Я попытался создать пример более понятным, и я изменил основное сообщение. – marielle