How to Save the Wordcloud in R

is there a way to save the data from wordcloud or comparison.cloud

The short answer is no, textplot_wordcloud() does not return a data object.

Suggestions:

  1. Use textstat_keyness() to get the differentially occurring words by group. This returns a data.frame that would work for your purposes. You could then use this as input for your plot, and possibly for wordcloud2. (See next suggestion.)

  2. Consider the wordcloud2 package, which also has Shiny (and interactive support). See https://github.com/quanteda/quanteda/issues/1218 for a discussion.

How to put a wordcloud in a pdf with a good quality

1) You run your code

#Data
word<-c("data","cloud","complessità","system",
"cliente","soglia","servizi","network","digitale",
"radio","progetto","scada","ticketing","telephone",
"web","app","business","engineering","requisiti",
"sistema")
freq<-c(33,32,29,29,24,24,19,18,17,17,15,15,15,14,14,13,13,13,13,13)
ds<-as.data.frame(cbind(word, freq))
ds$freq<-as.numeric(ds$freq)

library(wordcloud2)
set.seed(142)
wordcloud2(ds, size = 1.5, #widgetsize = 10,
minRotation = -pi/4, maxRotation = -pi/4)

2) In RStudio You can open your worldcloud in google chrome

Sample Image

3) You save the output of google chrome in PDF (CTRL+P ->Save -> PDF)

Sample Image

4) Download your high quality pdf output here:
https://www.docdroid.net/3tZaVGm/capturar-pdf.pdf

Saving text from webpage for word cloud in R

Here's one way:

library(rvest)
library(wordcloud)

test <- read_html("https://finearts.uvic.ca/writing/websites/writ218/screenplays/
award_winning/good_will_hunting.html")

text <- html_text(test)
content <- stringi::stri_extract_all_words(text, simplify = TRUE)

wordcloud(content, min.freq = 10, colors = RColorBrewer::brewer.pal(5,"Spectral"))

Which gives:

Sample Image

How to view and save the WordCloud generated in Python

First, there are some errors in your code:

1. The .csv file must be placed in a list for reading:

with open('C:/Users/Sushma/Downloads/wordcloud.csv', 'r', encoding = 'utf8', newline = '\r\n') as file:
reader = csv.reader(file, skipinitialspace = False, delimiter = ',', quoting = csv.QUOTE_NONE)
data = list(reader)

2. After this, a dictionary must be created that contains the key and the value in int format so that later, we can create the WordCloud:

word_freq = {}
for k, v in data[1:]:
word_freq[k] = int(v)

Now, you need to create a variable that will receive the multiplication between the text and its frequency:

text = ' '.join([(k + ' ') * v for k, v in word_freq.items()])

Then, you will create the WordCloud that will receive this variable text:

wordcloud = WordCloud(width = 1600,
height = 800,
background_color = 'white',
collocations = False,
repeat = True).generate_from_text(text)

plt.figure(figsize=(10,5))
plt.imshow(wordcloud, interpolation = "bilinear")
plt.axis('off')

And now you can simply save the generated WordCloud:

plt.savefig(f'wordcloud.png',
dpi = 300)

To plot (visualize) just use:

plt.show()

Final result:

Final result

Export wordcloud2 as html automatically?

require(wordcloud2)
df <- head(demoFreq, 5)
my_cloud <- wordcloud2(df)
my_path <- htmltools::html_print(my_cloud) # saves html in temp directory
print(my_path) # the location of the html file.

It would be best to swiftly move this file to a more permanent home.

create wordcloud output using loops in R

No need to write two separate loops, read the data and prepare wordcloud in same loop.

ldf <- lapply(listtxt, function(x) {
df <- read.delim(x)
wordcloud::wordcloud(df$name, df$freq)
#If the column name is not same in all the text files
#you can also refer columns by their position
#wordcloud::wordcloud(df[[1]], df[[2]])
})

Add other parameters (like random.order = FALSE) to wordcloud function based on your requirement. ldf would have list of wordclouds.



Related Topics



Leave a reply



Submit