Wordcloud с конкретной буквой - PullRequest
0 голосов
/ 15 апреля 2019

Я создал Wordcloud, который содержит упоминания в Твиттере за февраль без проблем.Тем не менее, я хочу иметь возможность редактировать его, во-первых, я попытался создать облако слов с помощью логотипа Twitter, и когда это было безуспешно, я попытался создать облако с использованием буквы 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
...