使用R在网络定制上苦苦挣扎

问题描述 投票:0回答:1

我很没有实验,但我正在尝试绘制一个共同作者的船舶网络并在R中进行分析。

我可以建立网络,但我想改变可视化。我在http://eiko-fried.com/create-your-collaborator-network-in-r/上找到了一个代码并尝试将它与我的相结合。现在他说:“警告:忽略未知的美学:标签”任何人都可以查看我的代码并告诉我什么是错的。我非常感谢任何帮助指南!

library(igraph)
# Concept (for a single publication)
str1 <- "Mueller, Meier, Max"
a <- strsplit(str1, ";")[[1]]
a <- trimws(a)
a
mta <- matrix(1, length(a), length(a))
colnames(mta) <- a
rownames(mta) <- a
diag(mta) <- 0
ga <- graph_from_adjacency_matrix(mta, mode = "undirected")
as_edgelist(ga)
# Apply to all publications 
author_strings <- c("Adamovic, M.; Branger, F.; Braud, Isabelle; Kralisch, S.","Anagnostou, Marios N.; Kalogiros, John; Anagnostou, Emmanouil N.; Tarolli, Michele; Papadopoulos, Anastasios; Borga, Marco",
                    "Anquetin, Sandrine; Braud, Isabelle; Vannier, Olivier; Viallet, Pierre; Boudevillain, Brice; Creutin, Jean-Dominique; Manus, Claire",
                    "Bezak, Nejc; Šraj, Mojca; Mikoš, Matjaž", 
                    "Biancamaria, Sylvain; Bates, Paul D.; Boone, Aaron; Mognard, Nelly M.",
                    "Blöschl, Günter; RESZLER, C.; KOMMA, J.", 
                    "Bonnifait, Laurent; Delrieu, Guy; Le Lay, Matthieu; Boudevillain, Brice; Masson, Arielle; Belleudy, Philippe; Gaume, Eric; Saulnier, Georges-Marie", 
                    "Borga, Marco; Boscolo, Paolo; Zanon, Francesco; Sangati, Marco",
                    "Borga, Marco; Gaume, Eric; Creutin, Jean-Dominique; Marchi, Lorenzo", 
                    "Boudou, M.; Lang, M.; Vinet, F.; Cœur, D.", "Bout, B.; Jetten, V. G.","Braud, Isabelle; Roux, Hélène; Anquetin, Sandrine; Maubourguet, Marie-Madeleine; Manus, Claire; Viallet, Pierre; Dartus, Denis","Camarasa-Belmonte, Ana M.", "Collier, C. G.", "Dehotin, J.; Braud, Isabelle", "Douinot, Audrey; Roux, Hélène; Garambois, Pierre-André; Larnier, Kévin; Labat, David; Dartus, Denis",
                    "Estupina-Borrell, V.; Dartus, Denis; Ababou, R.", "Fares, Ali; Awal, Ripendra; Michaud, Jene; Chu, Pao-Shin; Fares, Samira; Kodama, Kevin; Rosener, Matt",
                    "Forestieri, A.; Caracciolo, D.; Arnone, E.; Noto, L. V.", "Garambois, Pierre-André; Roux, Hélène; Larnier, Kévin",
                    "Garambois, Pierre-André; Roux, Hélène; Larnier, Kévin; Labat, David; Dartus, Denis", "García-Pintado, Javier; Barberá, Gonzalo G.; Erena, Manuel; Castillo, Victor M.",
                    "Gaume, Eric; Bain, Valerie; Bernardara, Pietro; Newinger, Olivier; Barbuc, Mihai; Bateman, Allen; Blaškovičová, Lotta; Blöschl, Günter; Borga, Marco; Dumitrescu, Alexandru; Daliakopoulos, Ioannis; Garcia, Joachim; Irimescu, Anisoara; Kohnova, Silvia; Koutroulis, Aristeidis; Marchi, Lorenzo; Matreata, Simona; Medina, Vicente; Preciso, Emanuele; Sempere-Torres, Daniel; Stancalie, Gheorghe; Szolgay, Jan; Tsanis, Ioannis; Velasco, David; Viglione, Alberto",
                    "Georgakakos, Konstantine P.", "Gourley, Jonathan J.; Erlingis, J. M.; Smith, T. M.; Ortega, K. L.; Hong, Yang", "Gourley, Jonathan J.; Flamig, Zachary L.; Hong, Yang; Howard, Kenneth W.",
                    "Grillakis, M. G.; Koutroulis, Aristeidis; KOMMA, J.; Tsanis, Ioannis; Wagner, W.; Blöschl, Günter", 
                    "Grillakis, M. G.; Tsanis, Ioannis; Koutroulis, Aristeidis", "Hapuarachchi, H. A. P.; Wang, Q. J.; Pagano, T. C.", 
                    "Hardy, Jill; Gourley, Jonathan J.; Kirstetter, Pierre-Emmanuel; Hong, Yang; Kong, Fanyou; Flamig, Zachary L.", 
                    "Javelle, Pierre; Fouchier, Catherine; Arnaud, Patrick; Lavabre, Jacques", "Kim, Jongho; Warnock, April; Ivanov, Valeriy Y.; Katopodes, Nikolaos D.",
                    "Kobold, M.; Brilly, M.", "Kong, Fanyou; Xue, Ming; Thomas, W.; Wang, Yunheng; Brewster, Keith; Wang, Xuguang; Gao, Jidong; Weiss, Steven J.; Clark, Adam; Kian, Jack; Coniglio, Michael C.; Jun Du, Jun", "Laiolo, P.; Gabellani, S.; Rebora, Nicola; Rudari, R.; Ferraris, Luca; Ratto, S.; Stevenin, H.; Cauduro, M.",
                    "Le Lay, Matthieu; Saulnier, Georges-Marie", "Marchi, Lorenzo; Borga, Marco; Preciso, Emanuele; Gaume, Eric", "Marchi, Lorenzo; Borga, Marco; Preciso, Emanuele; Sangati, Marco; Gaume, Eric; Bain, Valerie; Delrieu, Guy; Bonnifait, Laurent; Pogacnik, Nejc")
as <- strsplit(author_strings, ";")
els <- lapply(as, function(x) {
  x <- trimws(x)
  mtx <- matrix(1, length(x), length(x))
  colnames(mtx) <- x
  rownames(mtx) <- x
  diag(mtx) <- 0

  gx <- graph_from_adjacency_matrix(mtx, mode = "undirected")
  as.data.frame(as_edgelist(gx))
})
global_el <- do.call(rbind, els)
global_el$V3 <- 1
global_graph <- graph_from_data_frame(global_el, directed = FALSE)
plot(global_graph)

library(ggplot2)
# cleaning network data
network <- graph_from_data_frame(global_el, directed=FALSE)
set.seed(123)
l <- layout.fruchterman.reingold(network, niter=1500) # layout
fc <- walktrap.community(network) # community detection

# node locations
nodes <- data.frame(l); names(nodes) <- c("x", "y")
nodes$cluster <- factor(fc$membership)
nodes$label <- fc$names
nodes$degree <- degree(network)

# edge locations
edgelist <- get.edgelist(network, names=FALSE)
edges <- data.frame(nodes[edgelist[,1],c("x", "y")], nodes[edgelist[,2],c("x", "y")])
names(edges) <- c("x1", "y1", "x2", "y2")

# and now visualizing it...
p <- ggplot(nodes, aes(x=x, y=y, color=cluster, label=label, size=degree))
pq <- p + geom_text(color="black", aes(label=label, size=degree),
                    show.legend =FALSE) +
  # nodes
  geom_point(color="grey20", aes(fill=cluster),
             shape=21, show.legend =FALSE, alpha=1/2) +
  # edges
  geom_segment(
    aes(x=x1, y=y1, xend=x2, yend=y2, label=NA),
    data=edges, size=0.25, color="grey20", alpha=1/5) +
  ## note that here I add a border to the points
  scale_fill_discrete(labels=labels) +
  scale_size_continuous(range = c(5, 8)) +
  theme(
    panel.background = element_rect(fill = "white"),
    plot.background = element_rect(fill="white"),
    axis.line = element_blank(), axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(), panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.background = element_rect(colour = F, fill = "black"),
    legend.key = element_rect(fill = "black", colour = F),
    legend.title = element_text(color="white"),
    legend.text = element_text(color="white")
  ) +
  ## changing size of points in legend
  guides(fill = guide_legend(override.aes = list(size=5)))

pq
r networking ggplot2
1个回答
0
投票

如下更改您的代码,这将消除警告。你只需要将label美学添加到geom_text

p <- ggplot(nodes, aes(x=x, y=y, color=cluster, size=degree))

pq <- p + geom_text(color="black", aes(label=label, size=degree),
                show.legend =FALSE) +
# nodes
geom_point(color="grey20", aes(fill=cluster),
           shape=21, show.legend =FALSE, alpha=1/2) +
# edges
geom_segment(
    aes(x=x1, y=y1, xend=x2, yend=y2),
    data=edges, size=0.25, color="grey20", alpha=1/5) +
## note that here I add a border to the points
scale_fill_discrete(labels=labels) +
scale_size_continuous(range = c(5, 8)) +
theme(
    panel.background = element_rect(fill = "white"),
    plot.background = element_rect(fill="white"),
    axis.line = element_blank(), axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(), panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.background = element_rect(colour = F, fill = "black"),
    legend.key = element_rect(fill = "black", colour = F),
    legend.title = element_text(color="white"),
    legend.text = element_text(color="white")
) +
## changing size of points in legend
guides(fill = guide_legend(override.aes = list(size=5)))

pq
© www.soinside.com 2019 - 2024. All rights reserved.