-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_scripts_redes.R
executable file
·117 lines (84 loc) · 3.18 KB
/
3_scripts_redes.R
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -- José Luis Losada Palenzuela -- #
# -- 2021 -- #
### ------------------------------------------- ###
### Código para el curso de ###
### tratamiento y visualización de datos ###
### ------------------------------------------- ###
# -- 3_scripts_redes.R -- #
# -- Aquí solo se recoge el código de forma esquemática -- #
rm(list = ls()) # Limpia los datos de la sesión
# ¿En otra sesión? Cargue las librerías y los datos
library(tidyverse)
bibliocurso = read.csv("asignatura_sample.csv", encoding = "UTF-8", stringsAsFactors=F)
# Procesar los datos----
edgelist = select(bibliocurso, Author, Manual.Tags)
edgelist = separate_rows(edgelist, sep = ";", Manual.Tags)
edgelist = separate_rows(edgelist, sep = ";", Author)
edgelist = separate(edgelist, Author, into = c("apellido", "nombre"), sep = ",")
edgelist$nombre <- NULL
edgelist = na_if(edgelist, "")
edgelist = na.omit(edgelist)
edgelist = data.frame(lapply(edgelist, str_trim))
edgelist[1:3,]
# Visualizar con un diagrama de flujo----
# install.packages("ggalluvial")
library(ggalluvial)
ggplot(edgelist) +
aes(axis1 = Manual.Tags, axis2 = apellido) + # En el eje horizontal
geom_alluvium(aes(fill = Manual.Tags)) + # Conexiones y color
geom_stratum() + # Columnas
geom_text(stat = "stratum", aes(label = after_stat(stratum))) + # Texto y cálculo
theme_void() +
theme(legend.position = "none")
## Crear un objeto de red----
# install.packages("igraph")
library(igraph)
graph <- graph.data.frame(edgelist, directed = F)
# Visualizar con plot----
plot(graph,
vertex.label = NA)
plot(graph,
vertex.label.color="black",
vertex.shape="none")
is.bipartite(graph) # Comprobar si es un grafo bipartito
V(graph)$type <- V(graph)$name %in% edgelist[,2] # Crear un objeto bipartito
V(graph)$type
is.bipartite(graph)
plot(graph,
vertex.label = NA,
vertex.size = 7,
# layout = layout_as_bipartite,
vertex.color = ifelse(V(graph)$type, "tomato", "green")
)
# Calcular el grado de los nodos----
deg = degree(graph)
plot(graph,
vertex.label = NA,
vertex.size = deg*2,
# layout = layout_as_bipartite,
vertex.color = ifelse(V(graph)$type, "tomato", "green")
)
plot(graph,
vertex.label = ifelse(deg > 4, V(graph)$name, NA),
vertex.size = deg*2,
vertex.color = ifelse(V(graph)$type, "tomato", "green"),
vertex.label.cex = deg/4,
vertex.label.color = "black"
)
## Ejemplo de proyecto
# https://editio.github.io/mapping.literature/spatialnet.html
## Exportar los datos fuera de R----
# Aristas y nodos por separado
aristas = get.data.frame(graph, what = "edges")
nodos = get.data.frame(graph, what = "vertices")
write.csv(aristas, file = "aristas_biblio.csv")
write.csv(nodos, file = "nodos_biblio.csv")
# En formato gml (gephi)
V(graph)$deg = deg # Añade el cálculo del degree al objeto graph
write_graph(graph, "dataviz.gml", format = c("gml")) # Exporta en formato gml Gephi
# En formato XML, gexf (gephi)
library(rgexf)
g1.gexf <- igraph.to.gexf(graph) # convierte el objeto graph a gexf
f <- file("dataviz.gexf") # Crea el archivo y lo exporta.
writeLines(g1.gexf$graph, con = f)
close(f)