getwd()
setwd("C:/Users/.../R&D League")
install.packages("remotes")
remotes::install_github("gastonstat/arcdiagram")
library(arcdiagram)
install.packages("igraph")
library(igraph)
## Warning: package 'igraph' was built under R version 4.2.3
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(data.table)
# location of 'gml' file
mis_file = "OIRreach.txt"
# read 'gml' file
mis_graph = read.graph(mis_file, format="gml")
# get edgelist
edgelist = get.edgelist(mis_graph)
# get vertex labels
vlabels = get.vertex.attribute(mis_graph, "label")
# get vertex groups
vgroups = get.vertex.attribute(mis_graph, "group")
# get vertex fill color
vfill = get.vertex.attribute(mis_graph, "fill")
# get vertex border color
vborders = get.vertex.attribute(mis_graph, "border")
# get vertex degree
degrees = degree(mis_graph)
# get edges value
values = get.edge.attribute(mis_graph, "value")
install.packages("reshape")
# load reshape
library(reshape)
## Warning: package 'reshape' was built under R version 4.2.3
##
## Attaching package: 'reshape'
## The following object is masked from 'package:data.table':
##
## melt
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
## ✖ dplyr::between() masks data.table::between()
## ✖ purrr::compose() masks igraph::compose()
## ✖ tidyr::crossing() masks igraph::crossing()
## ✖ tidyr::expand() masks reshape::expand()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks data.table::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks data.table::last()
## ✖ dplyr::rename() masks reshape::rename()
## ✖ purrr::simplify() masks igraph::simplify()
## ✖ purrr::transpose() masks data.table::transpose()
# data frame with vgroups, degree, vlabels and ind
x = data.frame(vgroups, degrees, vlabels, ind=1:vcount(mis_graph))
# arranging by vgroups and degrees
y = arrange(x, desc(vgroups), desc(degrees))
# get ordering 'ind'
new_ord = y$ind
(add tidyverse: https://stackoverflow.com/questions/52456269/use-reshape2-to-reorder-the-first-columns-of-a-data-frame)
The values here are NOT accurate, this is a test diagram for the COSA Departments
# plot arc diagram
arcplot(edgelist, ordering=new_ord, labels=vlabels, cex.labels=0.8,
show.nodes=TRUE, col.nodes=vborders, bg.nodes=vfill,
cex.nodes = log(degrees)+0.5, pch.nodes=21,
lwd.nodes = 2, line=-0.5,
col.arcs = hsv(0, 0, 0.2, 0.25), lwd.arcs = 1.5 * values)
The values here are NOT accurate, this is a test diagram for the COSA Departments
# create a star graph with 10 nodes
star_graph = graph.star(10, mode="out")
# extract edgelist
star_edges = get.edgelist(star_graph)
# inspect star_edges
star_edges
## [,1] [,2]
## [1,] 1 2
## [2,] 1 3
## [3,] 1 4
## [4,] 1 5
## [5,] 1 6
## [6,] 1 7
## [7,] 1 8
## [8,] 1 9
## [9,] 1 10
# plot 1: default arc diagram
arcplot(star_edges)
# plot 2: show nodes as circles, in decreasing order
arcplot(star_edges, show.nodes=TRUE, sorted=TRUE, decreasing=TRUE, las=1)
# plot 3: different ordering, arc widths, arc colors, and node sizes
set.seed(120)
arcplot(star_edges, ordering=sample(1:10), labels=paste("node",1:10,sep="-"),
lwd.arcs=4*runif(10,.5,2), col.arcs=hsv(runif(9,0.6,0.8),alpha=0.4),
show.nodes=TRUE, pch.nodes=21, cex.nodes=runif(10,1,3),
col.nodes="gray80", bg.nodes="gray90", lwd.nodes=2)
# plot 4: same as plot 3 but vertically oriented
set.seed(120)
op = par(mar = c(0.5, 5, 0.5, 3))
arcplot(star_edges, ordering=sample(1:10), horizontal=FALSE,
labels=paste("node",1:10,sep="-"),
lwd.arcs=4*runif(10,.5,2), col.arcs=hsv(runif(9,0.6,0.8),alpha=0.4),
show.nodes=TRUE, pch.nodes=21, cex.nodes=runif(10,1,3),
col.nodes="gray80", bg.nodes="gray90", lwd.nodes=2)
par(op)
# Create a graph object with igraph
mygraph <- graph_from_data_frame( connect, vertices = coauth, directed = FALSE )
# prepare a vector of n color in the viridis scale
mycolor <- colormap(colormap=colormaps$viridis, nshades=max(coauth$grp))
mycolor <- sample(mycolor, length(mycolor))
# Make the graph
ggraph(mygraph, layout="linear") +
geom_edge_arc(edge_colour="black", edge_alpha=0.2, edge_width=0.3, fold=TRUE) +
geom_node_point(aes(size=n, color=as.factor(grp), fill=grp), alpha=0.5) +
scale_size_continuous(range=c(0.5,8)) +
scale_color_manual(values=mycolor) +
geom_node_text(aes(label=name), angle=65, hjust=1, nudge_y = -1.1, size=2.3) +
theme_void() +
theme(
legend.position="none",
plot.margin=unit(c(0,0,0.4,0), "null"),
panel.spacing=unit(c(0,0,3.4,0), "null")
) +
expand_limits(x = c(-1.2, 1.2), y = c(-5.6, 1.2))