Построим сеть с использованием алгоритма neighborNet.
Для начала загрузим необходимые библиотеки и приведем в порядок данные:
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.6.0
✔ ggplot2 4.0.0 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Loading required package: ape
Attaching package: 'ape'
The following object is masked from 'package:dplyr':
where
library (philentropy)
library (ggraph)
library (ggsci)
Warning: package 'ggsci' was built under R version 4.5.2
data <- read.table ("table_with_frequencies.txt" , header = TRUE , sep = " " )
data <- t (as.matrix (data))
data <- as.data.frame.matrix (data) |>
scale ()
Рассчитаем косинусное расстояние между текстами:
dist_mx <- data |>
philentropy:: distance (method = "cosine" , use.row.names = TRUE )
Metric: 'cosine' with unit: 'log'; comparing: 30 vectors
dist_mx <- as.dist (1 - dist_mx)
Зададим параметры для графика и построим сеть:
par (mar = c (5 ,5 ,5 ,5 ), cex = 0.8 )
nnet <- neighborNet (dist_mx)
cols <- pal_igv ()(12 )
# каждого автора обозначим отдельным цветом
cols <- tibble (author = str_remove (nnet$ tip.label, "_.+" )) |>
mutate (color = case_when (author == "Крюков" ~ cols[1 ],
author == "Фурманов" ~ cols[2 ],
author == "Леонов" ~ cols[3 ],
author == "Севский" ~ cols[4 ],
author == "Островский" ~ cols[5 ],
author == "Булгаков" ~ cols[6 ],
author == "Фадеев" ~ cols[7 ],
author == "Серафимович" ~ cols[8 ],
author == "Шолохов" ~ cols[9 ],
author == "Платонов" ~ cols[10 ],
author == "Иванов" ~ cols[11 ],
author == "Dubia" ~ cols[12 ]))
plot (nnet,
direction = "axial" ,
edge.color = "grey30" ,
use.edge.length = FALSE ,
edge.width = 1 ,
tip.color = cols$ color,
main = 'NeighborNet' )
Примечания
На графике мы отчетливо видим группировку текстов по авторам. «Тихий Дон», как это продемонстрировано, ближе всего оказывается к произведениям Шолохова.