如何将wordcloud放入质量好的pdf中

时间:2017-02-27 16:07:26

标签: r pdf word-cloud

我正在使用R包wordcloud2。它工作正常,但我需要生成一个带有结果的pdf,我找到的唯一方法如下。 我已经获得了包含单词和相应频率的数据框:

> 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

现在

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)

一旦我创建了wordcloud2对象(注意:我找不到widgetsize的好值。每次使用它时,我都会获得一个空图像。也许我可以用它来创建一个更高的定义对象),我把它放在一个HTML,然后我转换为PDF格式的HTML:

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

我获得了一张低分辨率的照片:

enter image description here

我可以在程序的哪个部分为最终图像设置更高的尺寸?我想采用我的wordcloud2图表(my_graph)并直接创建一个包含我需要的尺寸的pdf(或png等)。有办法吗?

2 个答案:

答案 0 :(得分:1)

1)您运行代码

#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)在RStudio中你可以在google chrome中打开你的worldcloud

enter image description here

3)您以PDF格式保存Google Chrome的输出(CTRL + P - &gt;保存 - &gt; PDF)

enter image description here

4)在此处下载高质量的pdf输出: https://www.docdroid.net/3tZaVGm/capturar-pdf.pdf

答案 1 :(得分:1)

webshot 函数的selector参数设置为'#canvas'可获得更好的结果。

因此,在您的情况下,它将是:

webshot(
    url = "myDocument.html",
    file = "myFigure.pdf", 
    delay = 6, 
    vwidth = 1500, 
    vheight = 1500,
    selector = '#canvas'
)