Use R to generate wordcloud-from Twitter Project

Source: Internet
Author: User

The following example is from Twitter, where R is used to mine information from Twitter. In fact, there is also rweibo in Chinese. You can study it if you have time.

Https://sites.google.com/site/miningtwitter/questions/talking-about/wordclouds

There are a total of three detailed introductions, which may need to go beyond GFW

The following is an example of the first article:

Https://sites.google.com/site/miningtwitter/questions/talking-about/wordclouds/wordcloud1

SImple WOrdcloud wordclouds

 

 

A wordcloud can be one of the best tools that allows us to visualize most of the words and terms contained in tweets. although its main use is for exploratory purposes, they have the advantage to be understandable by most users, and to be abstrally attractive to the human eyes (if done adequately ).

How to Create a wordcloud? Wordclouds are relatively simple to make. here's the main recipe stepsdownload some tweets (via Twitter or XML) extract the text from the tweetsdo some text manipulation (for cleaning & formatting) create a lexical corpus and a termdocumentmatrix (via TM) Obtain Words and Their frequenciesplot the wordcloud Example 1: tweets via TwitterStep 1:Load all the required packages
library(twitteR)library(tm)library(wordcloud)library(RColorBrewer)

 

Step 2:Let's get some tweets in English containing the words "Machine Learning"
mach_tweets = searchTwitter("machine learning", n=500, lang="en")

 

Step 3:Extract the text from the tweets in a vector
mach_text = sapply(mach_tweets, function(x) x$getText())

 

Step 4:Construct the lexical corpus and the term document matrixwe use the function corpus to create the corpus, and the function vectorsource to indicate that the text is in the Character Vector mach_text. in order to create the term-document matrix we apply different transformation such as removing numbers, punctuation symbols, lower case, etc.
# create a corpusmach_corpus = Corpus(VectorSource(mach_text)) # create document term matrix applying some transformationstdm = TermDocumentMatrix(mach_corpus,   control = list(removePunctuation = TRUE,   stopwords = c("machine", "learning", stopwords("english")),   removeNumbers = TRUE, tolower = TRUE)) 

 

Step 5:Obtain Words and Their Frequencies
# define tdm as matrixm = as.matrix(tdm)# get word counts in decreasing orderword_freqs = sort(rowSums(m), decreasing=TRUE) # create a data frame with words and their frequenciesdm = data.frame(word=names(word_freqs), freq=word_freqs)  

Step 6:Let's plot the wordcloud

 
# plot wordcloudwordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2")) # save the image in png formatpng("MachineLearningCloud.png", width=12, height=8, units="in", res=300)wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"))dev.off()

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.