Wordcloud with r
- 8 years ago
When I tried the original code, it worked with older versions of R, 3.2.3 but not newer versions of R, 3.3.1 and 3.4.2. The Corpus construction was returning only numbers which were then stripped out by the rest of the code causing problems. So, I hacked together a variation of the original code based upon comments and have a working version here:
require(tm) require(wordcloud) require(RColorBrewer) datain = as.data.frame(table(as.character(dataset[,1]))) words <- Corpus(VectorSource(dataset$text)) words <- tm_map(words, stripWhitespace) words <- tm_map(words, content_transformer(tolower)) words <- tm_map(words, removeNumbers) words <- tm_map(words, removePunctuation) words <- tm_map(words, removeWords, stopwords("english")) words <- tm_map(words, stemDocument) wordcloud(words, scale=c(5,0.75), max.words=50, random.order=FALSE, rot.per=0.35, use.r.layout=FALSE, colors=brewer.pal(8, "Dark2"))The main change is the construction of the Corpus, VectorSource(dataset) becomes VectorSource(dataset$text)
This is based upon the original PBIX file included in the original post.
Hi,
Nice post :-)
Do you know how to make a word cloud out of a columns (the entire content) for example i have a colum with company names and number of cases?
The problem is that by altering the Dataset I cannot use the code again in Power BI ( I had to write colClasses = c("character", "numeric"), to tell R that my dataset has 2 type of columns.
Thanks!
> `dataset` = read.csv('C:/Users/PatricioLobos/AppData/Local/Radio/REditorWrapper_8fe60428-ecc0-478f-ab77-56803f9407f4/input_df_227d85ab-962e-4701-b1ac-ff6e53817c56.csv', colClasses = c("character", "numeric"), check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
+ # Original Script. Please update your script content here and once completed copy below section back to the original editing window #
>require(wordcloud)
> require(RColorBrewer)
> pal2 <- brewer.pal(8, 'Dark2')
> plot(wordcloud(dataset$Kundenavn, dataset$`Nr. Cases`, scale = c(8, .4), min.freq = 5, max.words = Inf, random.order = FALSE, rot.per = .15, colors = pal2))
- boefraty9 years agoMicrosoft Employee
require(wordcloud) require(RColorBrewer) datain = as.data.frame(table(as.character(dataset[,2]))) pal2 <- brewer.pal(4,"Dark2") wordcloud(datain[,1],datain$Freq, scale=c(4,.3), min.freq=1, max.words=Inf, random.order=FALSE, rot.per=.15, colors=pal2)The dataset is 2 columns: "ID", "Term" . If you have "Frequency" instead of "ID" no need to call "table" operator