/
5-CreateGraph.rmd
75 lines (61 loc) · 2.4 KB
/
5-CreateGraph.rmd
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
---
title: "CreateGraph"
author: "Mango117"
date: "02 March 2023"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
```{r tdm}
#For this graph, we are using the test results from the AALbi Dictionary + Topic Modelled pdfs
setwd("/Users/manojarachige/Library/CloudStorage/Dropbox/Mac/Documents/Coding/Projects/BMedScDOC2021/BMedScDOC_2023")
tdm1 <- read.csv(file = "/Users/manojarachige/Library/CloudStorage/Dropbox/Mac/Documents/Coding/Projects/BMedScDOC2021/BMedScDOC_2023/Outputs/Graphical_Analysis/AbstractsNMF-TDM.csv")
tdm1 <- tdm1[,-c(1)]
tdm1 <- t(tdm1)
m <- as.matrix(tdm1)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 10)
```
```{r wordcloud}
library(wordcloud)
set.seed(1234)
wordcloud(words = d$word, freq = d$freq, min.freq = 1,
max.words=200, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))
barplot(d[1:20,]$freq, las = 2, names.arg = d[1:20,]$word,
col ="lightblue", main ="Most frequent words",
ylab = "Word frequencies")
```
```{r graph}
library(pubmed.mineR)
cos_sim_calc(tdm1) #output file cosimdata.txt #cytoscape
cos=read.table("cossimdata.txt",header=FALSE,sep="\t")
cos=na.omit(cos)
library(igraph)
relations <- data.frame(from=cos[,1], to=cos[,2], weight=abs(cos[,3]))
relations2=relations[-row(relations)[relations == 0],] #remove 0
#Building a directed graph
g.1a <- graph.data.frame(relations2, directed=FALSE)
V(g.1a)$size<-6
min<-.95 #threshold
layout1 <- layout.auto(g.1a)
#Give the graph lots of room
#opar <- par()$mar; par(mar=rep(3, 4))
#plot(g.1a, layout=layout1)
plot(g.1a, layout=layout1, edge.width=ifelse(E(g.1a)$weight>=min, E(g.1a)$weight, NA))
```
This graph is *DIRECTED*
```{r graphml create}
wc=cluster_walktrap(g.1a)
modularity(wc)
plot(wc,g.1a, layout=layout1) #remove vertex label
V(g.1a)$color<-wc$membership
#plot(g.1a,vertex.size=betweenness(g.1a))
#plot(g.1a,vertex.color=V(g.1a)$color,vertex.size=degree(g.1a))
write.graph(g.1a,"tdm_fulldict_graph.graphml",format = "graphml")
```