owwoow14 owwoow14 - 1 month ago 5
R Question

Equivalent of max.word parameter for WordCloud2 in R

I am trying to build a wordcloud using

Wordcloud2
in
R
in order to take advantage of Shiny.
One very important thing that I must do is maintain a fixed maximum number of words to represent in the wordcloud (for instance 150). Therefore, regardless of the number of words that are available I only want to visualize a maximum of 150 words in the cloud.
In
Wordcloud
, this parameter
max.words=
is available. However, I am having trouble finding something equivalent for
Wordcloud2
that allows me to add this parameter. I have done a lot of research regarding this and cannot seem to find it. My code for
Wordcloud2
is very simple

mr = table(data, header=TRUE)
wordcloud2(mr, color = ifelse(mr[, 3] > 0, 'green', 'red'), fontWeight = "bold")


Is there any parameter that I can add such as
max.words=
(available in
Wordcloud
)to guarantee that no more that X words appear in my cloud?

My data is 3 columns, such as:

WORD VALUE SENT
1 topnotch 1 1
2 good 2 1
3 nice 11 0
4 inspired 14 0
5 beautiful 21 0

Answer

I don't see an argument that you can pass to accomplish that. I would just subset the data set before passing it to the wordcloud, like:

data <- data[order(data$Value, decreasing = TRUE), ]
pass_to_wordcloud <- data[1:150, ] #or set 150 to whatever