library(ggraph)
## Loading required package: ggplot2
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ✔ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidygraph)
##
## Attaching package: 'tidygraph'
## The following object is masked from 'package:stats':
##
## filter
library(igraph)
##
## Attaching package: 'igraph'
## The following object is masked from 'package:tidygraph':
##
## groups
## The following objects are masked from 'package:dplyr':
##
## as_data_frame, groups, union
## The following objects are masked from 'package:purrr':
##
## compose, simplify
## The following object is masked from 'package:tidyr':
##
## crossing
## The following object is masked from 'package:tibble':
##
## as_data_frame
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
# Create graph of highschool friendships
dim(highschool) #[1] 506 3
## [1] 506 3
head(highschool)
## from to year
## 1 1 14 1957
## 2 1 15 1957
## 3 1 21 1957
## 4 1 54 1957
## 5 1 55 1957
## 6 2 21 1957
edges <- highschool
edges <- edges %>%
group_by(year) %>%
sample_n(size=10)
edges
## # A tibble: 20 × 3
## # Groups: year [2]
## from to year
## <dbl> <dbl> <dbl>
## 1 23 53 1957
## 2 67 69 1957
## 3 35 48 1957
## 4 1 14 1957
## 5 54 49 1957
## 6 65 68 1957
## 7 47 53 1957
## 8 23 43 1957
## 9 11 52 1957
## 10 23 52 1957
## 11 12 20 1958
## 12 10 19 1958
## 13 17 7 1958
## 14 29 33 1958
## 15 8 28 1958
## 16 70 66 1958
## 17 68 62 1958
## 18 14 13 1958
## 19 20 19 1958
## 20 33 36 1958
edge_1 <- edges
str(edge_1)
## grouped_df [20 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
## $ from: num [1:20] 23 67 35 1 54 65 47 23 11 23 ...
## $ to : num [1:20] 53 69 48 14 49 68 53 43 52 52 ...
## $ year: num [1:20] 1957 1957 1957 1957 1957 ...
## - attr(*, "groups")= tibble [2 × 2] (S3: tbl_df/tbl/data.frame)
## ..$ year : num [1:2] 1957 1958
## ..$ .rows: list<int> [1:2]
## .. ..$ : int [1:10] 1 2 3 4 5 6 7 8 9 10
## .. ..$ : int [1:10] 11 12 13 14 15 16 17 18 19 20
## .. ..@ ptype: int(0)
## ..- attr(*, ".drop")= logi TRUE
nodes <- data.frame(name = unique(union(edges$from, edges$to)))
ig <- graph_from_data_frame(d = edge_1, vertices = nodes)
ig_tidy <- as_tbl_graph(ig)
ig
## IGRAPH 6ff6991 DN-- 31 20 --
## + attr: name (v/c), year (e/n)
## + edges from 6ff6991 (vertex names):
## [1] 23->53 67->69 35->48 1 ->14 54->49 65->68 47->53 23->43 11->52 23->52
## [11] 12->20 10->19 17->7 29->33 8 ->28 70->66 68->62 14->13 20->19 33->36
#######################################
par(mfrow= c(1, 3))
plot(create_star(10))
plot(create_ring(10))
plot(create_tree(n = 20, children = 3), edge.arrow.size = .4)

#####################################
par(mfrow= c(1, 3))
plot(play_geometry(6, 1, torus = FALSE))
plot(play_islands(1, 5, 0.7, 3))
plot(play_forestfire(5, 0.5), edge.arrow.size = .2)

####################################
ggraph(ig) +
geom_edge_link() +
geom_node_point() +
theme_graph()
## Multiple parents. Unfolding graph
## Multiple roots in graph. Choosing the first
## Multiple parents. Unfolding graph
## Multiple roots in graph. Choosing the first
## Using `tree` as default layout

ggraph(ig, layout = 'kk') +
geom_edge_link() +
geom_node_point() +
theme_graph()

ggraph(ig, layout = 'kk', maxiter = 100) +
geom_edge_link() +
geom_node_point() +
theme_graph()

#######################################
#library(extrafont)
#extrafont::font_import()
#loadfonts(dev="win")
#########################################
ggraph(ig, layout = 'linear') +
geom_edge_arc(aes(colour = factor(year))) +
geom_node_point() +
theme_graph()

######################################
ggraph(ig, layout = 'linear', circular = TRUE) +
geom_edge_arc(aes(colour = factor(year))) +
geom_node_point() +
coord_fixed() +
theme_graph()

?geom_node_point
#####################################
graph <- tbl_graph(flare$vertices, flare$edges)
ggraph(graph, 'partition') +
geom_node_tile(aes(fill = depth), size = 0.25) +
theme_graph()

############
ggraph(graph, 'partition', circular = TRUE) +
geom_node_arc_bar(aes(fill = depth), size = 0.25) +
coord_fixed() +
theme_graph()

#############
ggraph(graph, 'circlepack', weight = size) +
geom_node_circle(aes(fill = factor(depth)), size = 0.25, n = 50) +
coord_fixed() +
theme_graph()

#############
ggraph(graph, 'treemap', weight = size) +
geom_node_tile(aes(fill = factor(depth)), size = 0.25) +
theme_graph()

###########
ggraph(graph, 'tree') +
geom_edge_diagonal() +
theme_graph()

###########
dendrogram <- hclust(dist(iris[, 1:4]))
ggraph(dendrogram, 'dendrogram', height = height) +
geom_edge_elbow() +
theme_graph()

ggraph(dendrogram, 'dendrogram', circular = TRUE) +
geom_edge_elbow() +
coord_fixed() +
theme_graph()

# https://zhuanlan.zhihu.com/p/375171266