如何将wordcloud放入grob中?

rno*_*erg 2 r ggplot2 word-cloud gridextra r-grid

我创建了一个简单的wordcloud:

require(wordcloud)    
words <- c('affectionate', 'ambitious', 'anxious', 'articulate', 'artistic', 'caring', 'contented', 'creative', 'cynical', 'daring', 'dependable', 'easygoing', 'energetic', 'funny', 'generous', 'genuine', 'goodlistener', 'goodtalker', 'happy', 'hardworking', 'humerous', 'impulsive', 'intelligent', 'kind', 'loyal', 'modest', 'optimistic', 'outgoing', 'outrageous', 'passionate', 'perceptive', 'physicallyfit', 'quiet', 'rational', 'respectful', 'romantic', 'shy', 'spiritual', 'spontaneous', 'sweet', 'thoughtful', 'warm')
freqs <- c(134, 53, 0, 5, 0, 247, 0, 78, 0, 0, 134, 178, 79, 344, 63, 65, 257, 0, 109, 113, 0, 0, 107, 51, 199, 24, 67, 232, 0, 109, 24, 28, 29, 2, 105, 70, 0, 35, 64, 156, 66, 45)
wordcloud(words, freqs)
Run Code Online (Sandbox Code Playgroud)

我想将它放入"grob"中,以便我可以grid.arrange()gridExtra包中使用其他几个图来安排它:

require(ggplot2)
p1 <- qplot(1:10, rnorm(10), colour = runif(10))
require(gridExtra)
grid.arrange(p1, my.wordcloud)
Run Code Online (Sandbox Code Playgroud)

我知道我的wordcloud必须是一个"grob"才能做到这一点,但我不明白如何做到这一点.我尝试grob()gridExtra包中使用该功能,但这不起作用.建议?

42-*_*42- 8

编译代码并不难以wordcloud构建需要在网格中填写text.grob的数据.的wordcloud代码发送的x,y,文本和腐值到基text带的0,0和1限制的窗函数后,1被指定.

我需要在for循环之前添加它:

textmat <- data.frame(x1=rep(NA, length(words)), y1=NA, words=NA_character_, 
                       rotWord=NA, cexw=NA, stringsAsFactors=FALSE )
Run Code Online (Sandbox Code Playgroud)

这在for循环结束时:

 textmat[i, c(1,2,4,5) ] <-  c(x1=x1, y1=y1, rotWord=rotWord*90, cexw = size[i] )
 textmat[i, 3] <- words[i]
Run Code Online (Sandbox Code Playgroud)

并且需要修改调用.overlap,因为它显然没有导出:

if (!use.r.layout) 
         return(wordcloud:::.overlap(x1, y1, sw1, sh1, boxes))
Run Code Online (Sandbox Code Playgroud)

循环完成后我无形地返回了它:

return(invisible(textmat[-1, ]))  # to get rid of the NA row at the beginning
Run Code Online (Sandbox Code Playgroud)

命名后wordcloud2:

> tmat <- wordcloud2(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62))
> str(tmat)
'data.frame':   61 obs. of  5 variables:
 $ x1     : num  0.493 0.531 0.538 0.487 ...
 $ y1     : num  0.497 0.479 0.532 0.475 ...
 $ words  : chr  "b" "O" "M" ...
 $ rotWord: num  0 0 0 0 0 0 0 0 0 ...
 $ cexw   : num  0.561 2.796 2.682 1.421 ...

draw.text <- function(x,y,words,rotW,cexw) {
     grid.text(words, x=x,y=y, rot=rotW, gp=gpar( fontsize=9*cexw)) }

for(i in 1:nrow(tmat) ) { draw.text(x=tmat[i,"x1"], y=tmat[i,"y1"], 
                                    words=tmat[i,"words"], rot=tmat[i,"rotWord"], 
                                    cexw=tmat[i,"cexw"]) }
Run Code Online (Sandbox Code Playgroud)

建议:

 with(tmat, grid.text(x=x1, y=y1, label=words, rot=rotWord, 
                      gp=gpar( fontsize=9*cexw)) }  # untested
Run Code Online (Sandbox Code Playgroud)