Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

pdf

word-cloud

I am using the R package wordcloud2. It works fine, but I need to produce a pdf with the result and the only way I have found is the following. I have already obtained a dataframe with words and corresponding frequencies:

> ds
          word freq
1         data   33
2        cloud   32
3  complessità   29
4       system   29
5      cliente   24
6       soglia   24
7      servizi   19
8      network   18
9     digitale   17
10       radio   17
11    progetto   15
12       scada   15
13   ticketing   15
14   telephone   14
15         web   14
16         app   13
17    business   13
18 engineering   13
19   requisiti   13
20     sistema   13

Now

library(wordcloud2)
library(webshot)
library("htmlwidgets")
webshot::install_phantomjs()
set.seed(142)
my_graph  = wordcloud2(ds, size = 1.5, #widgetsize = 10,
                       minRotation = -pi/4, maxRotation = -pi/4)

Once I have created the wordcloud2 object (note: I cannot find good values for widgetsize. Every time I use it, I obtain an empty image. Maybe I could use it to create an higher defined object), I put it in an html, and then i convert the html in a pdf:

# sizingPolicy(defaultWidth = 100, ....) <- possible solution?
saveWidget(my_graph, "myDocument.html"  , selfcontained = F)
webshot("myDocument.html","myFigure.pdf", delay =6, vwidth = 1500, vheight=1500)

I obtain a picture with a low definition:

enter image description here

In which part of the procedure can I set an higher dimension for my final image? I would like to take my wordcloud2 graph (my_graph) and directly create a pdf (or png, etc.) with the dimensions I need. Is there a way?

like image 414
Andrea Ianni ௫ Avatar asked Oct 29 '25 14:10

Andrea Ianni ௫


1 Answers

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

enter image description here

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

enter image description here

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

like image 145
Jalles10 Avatar answered Oct 31 '25 02:10

Jalles10