用R生成wordcloud—— 来自于twitteR 项目

下面的例子来自于twitteR,用R从twitter中挖掘信息, 其实中文也有Rweibo,有时间可以研究一下。

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

共有三篇详细介绍,可能需要越过GFW

下面是第一篇的例子:

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 visually attractive to the human eyes (if done adequately).
 
How to create a wordcloud?
Wordclouds are relatively simple to make. Here's the main recipe steps
Download some tweets (via twitteR or XML)
Extract the text from the tweets
Do some text manipulation (for cleaning & formatting)
Create a lexical Corpus and a TermDocumentMatrix (via tm)
Obtain words and their frequencies
Plot the wordcloud
 
 
Example 1: tweets via twitteR
Step 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 Matrix
We 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 corpus
mach_corpus = Corpus(VectorSource(mach_text))
 
# create document term matrix applying some transformations
tdm = 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 matrix
m = as.matrix(tdm)
# get word counts in decreasing order
word_freqs = sort(rowSums(m), decreasing=TRUE) 
# create a data frame with words and their frequencies
dm = data.frame(word=names(word_freqs), freq=word_freqs)
  

Step 6: Let's plot the wordcloud

 
# plot wordcloud
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"))
 
# save the image in png format
png("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()

 

 
 

 

转载于:https://www.cnblogs.com/todoit/archive/2012/07/23/2605546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值