Giter Site home page Giter Site logo

wordcloud-script's Introduction

Wordcloud-Script

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:

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:

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:

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:

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:

wordcloud2(d)

Letterplot:

letterCloud(d, word = "EXAMPLE", color='random-light' , backgroundColor="black")

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.

#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)

wordcloud-script's People

Contributors

matthewsmith430 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.