Wordcloud with a specific letter

I have created a wordcloud which contains Twitter mentions for February no problem. However, I want to be able to edit it, firstly trying to create a wordcloud using the Twitter logo and when that was unsuccessful I tried making one using the letter T. However this was also unsuccessful and i'm not sure why or how to resolve the issue. I'll input the code below so you can see where I'm going wrong.

.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")

Below is what the error says when i try inputting the code to change the shape of the 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**

The object that you're passing to letterCloud(), corpus, hasn't undergone any of the transformations, since you assign the results to a new object, docs. According to the wordcloud2 docs, the first argument of letterCloud() needs to be a data frame with word and frequency.

Arguments

data A data frame including word and freq in each column

You can cast your TermDocumentMatrix or your corpus object into a data frame using some of the helpers from tidytext:

I can't tell without your input objects, but it looks like you might already have this ready to go as d.

Thank you!

I checked my inputs and saw 'd' was listed as a data frame with 2 variables I just missed it. It seems to be doing what I want it to now.

1 Like

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.