-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
193 lines (160 loc) · 7.24 KB
/
README.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
title: "Wordcloud-Script"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
R script to create wordcloud from text document, using the `wordcloud2` package.
## Example
Here is an example of how to create a wordcloud. I use my PhD thesis as the example text. My PhD thesis can be found at:
<https://www.researchgate.net/publication/319876899_Corporate_networks_of_international_investment_and_trade>
First, load the relevant libraries:
```{r LIB,eval=FALSE}
library(tm)
library(magrittr)
#library(devtools)
#devtools::install_github("lchiffon/wordcloud2")
library(wordcloud2)
```
Then I save the document as a .txt file and then read into R:
```{r Read,eval=FALSE}
text1<-"C:\\Users\\example.txt"
text2 <-readLines(text1)
DOCtxt<-Corpus(VectorSource(text2))
```
I then clean the document, removing whitespace, common words and numbers. I convert all words to lowercase. And finally, I remove a list of words - these are common words for this particular document:
```{r remove,eval=FALSE}
DOCtxt_data<-tm_map(DOCtxt,stripWhitespace)%>%tm_map(.,tolower)%>%
tm_map(.,removeNumbers)%>%tm_map(.,removePunctuation)%>%
tm_map(.,removeWords,stopwords("english"))%>%
tm_map(.,removeWords,c("and","the","our","that","table","figure",
"for","are","also","more","has",
"must","have","should","this",
"with","furthermore","PhD",
"additionally","in","number",
"which","patterns","use","one",
"approach","whether","capture",
"effects","specifically",
"instance","yet","two",
"rather","approaches","along",
"however","extent","provides",
"applied","characterised",
"amongst","need","often","order",
"table","based","key","examining",
"captures","important","positive","using",
"negative","better","perspective","journal",
"case","therefore","can","work","et","al",
"used","role","set","studies","measures",
"paper","provide","figures","tables"))
```
From this - we create an object providing words and frequency:
```{r DTM,eval=FALSE}
dtm<- TermDocumentMatrix(DOCtxt_data)%>%as.matrix()
v <- sort(rowSums(dtm),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
```
## Wordcloud Plot
Now I have the clean object, I can create the plots.
### Wordcloud plot:
```{r WC_command,eval=FALSE}
wordcloud2(d)
```
```{r WC, echo=FALSE}
library(NLP)
library(tm)
library(magrittr)
speech <-"C:\\Users\\matt_\\Documents\\Wordcloud\\CORPORATE NETWORKS OF INTERNATIONAL_WORDCLOUD.txt"
THESIStxt <-readLines(speech)
THESIS<-Corpus(VectorSource(THESIStxt))
THESIS_data2<-tm_map(THESIS,stripWhitespace)%>%tm_map(.,tolower)%>%
tm_map(.,removeNumbers)%>%tm_map(.,removePunctuation)%>%
tm_map(.,removeWords,stopwords("english"))%>%
tm_map(.,removeWords,c("and","the","our","that","table","figure",
"for","are","also","more","has",
"must","have","should","this",
"with","furthermore","PhD",
"additionally","in","number",
"which","patterns","use","one",
"approach","whether","capture",
"effects","specifically",
"instance","yet","two",
"rather","approaches","along",
"however","extent","provides",
"applied","characterised",
"amongst","need","often","order",
"table","based","key","examining",
"captures","important","positive","using",
"negative","better","perspective","journal",
"case","therefore","can","work","et","al",
"used","role","set","studies","measures",
"paper","provide","figures","tables"))
dtm<- TermDocumentMatrix(THESIS_data2)%>%as.matrix()
v <- sort(rowSums(dtm),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
library(wordcloud2)
PLOTcloud<-wordcloud2(d)
library(htmlwidgets)
library(webshot)
saveWidget(PLOTcloud,"wordcloud_example.html",selfcontained = F)
webshot(url = "wordcloud_example.html", file = "wordcloud_example.png",delay=30)
```
### Letterplot:
```{r LC_command,eval=FALSE}
letterCloud(d, word = "EXAMPLE", color='random-light' , backgroundColor="black")
```
```{r WL, echo=FALSE}
library(NLP)
library(tm)
library(magrittr)
speech <-"C:\\Users\\matt_\\Documents\\Wordcloud\\CORPORATE NETWORKS OF INTERNATIONAL_WORDCLOUD.txt"
THESIStxt <-readLines(speech)
THESIS<-Corpus(VectorSource(THESIStxt))
THESIS_data2<-tm_map(THESIS,stripWhitespace)%>%tm_map(.,tolower)%>%
tm_map(.,removeNumbers)%>%tm_map(.,removePunctuation)%>%
tm_map(.,removeWords,stopwords("english"))%>%
tm_map(.,removeWords,c("and","the","our","that","table","figure",
"for","are","also","more","has",
"must","have","should","this",
"with","furthermore","PhD",
"additionally","in","number",
"which","patterns","use","one",
"approach","whether","capture",
"effects","specifically",
"instance","yet","two",
"rather","approaches","along",
"however","extent","provides",
"applied","characterised",
"amongst","need","often","order",
"table","based","key","examining",
"captures","important","positive","using",
"negative","better","perspective","journal",
"case","therefore","can","work","et","al",
"used","role","set","studies","measures",
"paper","provide","figures","tables"))
dtm<- TermDocumentMatrix(THESIS_data2)%>%as.matrix()
v <- sort(rowSums(dtm),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
library(wordcloud2)
PLOTletter<-letterCloud(d, word = "EXAMPLE", color='random-light' , backgroundColor="black")
library(htmlwidgets)
library(webshot)
saveWidget(PLOTletter,"letter_example.html",selfcontained = F)
webshot(url = "letter_example.html", file = "letter_example.png",delay=120)
```
## Writing Plots to files
These plots output to html, where you can then save as an image in your web browser. Otherwise, you can save these as images directly in R, using `htmlwidget` and `webshot` packages.
```{r SAVE_image,eval=FALSE}
#install webshot
library(webshot)
webshot::install_phantomjs()
## Make the wordcloud
CLOUD<-wordcloud2(d)
## save it in html
library("htmlwidgets")
saveWidget(CLOUD,"CLOUD.html",selfcontained = F)
## Save as image (png, jpeg or pdf)
###It is important to specify a delay - as this gives time for the wordcloud to load fully in html
webshot("CLOUD.html","CLOUD_1.png", delay =15)
webshot("CLOUD.html","CLOUD_1.pdf", delay =15)
```