带有特定字母的Wordcloud

时间:2019-04-15 11:58:11

标签: r wordcloud2

我创建了一个wordcloud,其中包含2月份Twitter提及的内容。但是,我希望能够对其进行编辑,首先,我尝试使用Twitter徽标创建一个wordcloud,如果尝试失败,我尝试使用字母T进行创建。但是,这也不成功,我不确定为什么或如何做解决问题。我将在下面输入代码,以便您查看我要去哪里。

.libPaths("M:/R")
library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")
library("wordcloud2")
library("magrittr")
library("tidytext")

get_sentiments("afinn")
get_sentiments("bing")
get_sentiments("nrc")

#Loading the data as a corpus
corpus <- Corpus(DirSource(directory = "//ecfle35/STAFF-HOME$/MaxEmery/Twitter mentions"))

inspect(corpus)

#Remove special characters
toSpace <- content_transformer(function(x, pattern) gsub(pattern, "", x))
docs <- tm_map(corpus, toSpace, "/")
docs <- tm_map(corpus, toSpace, "@")
docs <- tm_map(corpus, toSpace, "\|")

#Convert the text to lower case
docs <- tm_map(docs, content_transformer(tolower))
#Remove numbers
docs <- tm_map(docs, removeNumbers)
#Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
#specify your stopwrods as a character vector
docs <- tm_map(docs, removeWords, c("exetercollege", "exeter", "execollsport", "pitchatpalace", "execolljapan", "day", "today", "sponsorship","thank", "http", "see", "https", "will", "exeapprentices", "business", "well", "first", "microsofteduk", "apprenticeships", "new", "looking", "team", "skills", "entrepreneurs", "microsoft"))
#Remove punctuations
docs <- tm_map(docs, removePunctuation)
#Get rid of extra white spaces
docs <- tm_map(docs, stripWhitespace)
#Text stemming (strip off common suffixes)
docs <- tm_map(docs, stemDocument)

#Build a term-document matrix (a table containing the frequency of the words)
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m), decreasing = TRUE)
d <- data.frame(word= names(v), freq=v)
head(d, 10)

#Generate the word cloud
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"))

#Change the shape of the wordcloud

letterCloud(corpus, word = "T", color='random-light' , backgroundColor="black")

#Change the shape using your image
wordcloud2(demoFreq, figpath= "twitter.png", size= 1.5, color= "skyblue", backgroundColor="black")

我希望能够使用我的语料库,以便为wordcloud创建不同的形状并使用不同的图像。

当我尝试输入以下代码时,我收到一条错误消息:

> letterCloud(corpus, word = "T", color='random-light' , backgroundColor="black")

Error in as.data.frame.default(data) : 
  cannot coerce class ‘c("SimpleCorpus", "Corpus")’ to a data.frame

0 个答案:

没有答案