我创建了一个简单的单词云:
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)我想把它放到一个"grob“中,这样我就可以使用gridExtra包中的grid.arrange()将它与其他几个绘图一起排列:
require(ggplot2)
p1 <- qplot(1:10, rnorm(10), colour = runif(10))
require(gridExtra)
grid.arrange(p1, my.wordcloud)我知道我的wordcloud必须是一个"grob“才能做到这一点,但我不明白如何做到这一点。我尝试使用gridExtra包中的grob()函数,但不起作用。有什么建议吗?
发布于 2013-03-23 16:54:12
调整wordcloud中的代码以构造网格中填充text.grob所需的数据应该不是那么困难。在指定了限制为0,0和1,1的窗口后,wordcloud代码将x,y,文本和ROT值发送到基本text函数。
我需要在for循环之前添加以下内容:
textmat <- data.frame(x1=rep(NA, length(words)), y1=NA, words=NA_character_,
rotWord=NA, cexw=NA, stringsAsFactors=FALSE )在for循环的末尾:
textmat[i, c(1,2,4,5) ] <- c(x1=x1, y1=y1, rotWord=rotWord*90, cexw = size[i] )
textmat[i, 3] <- words[i]并且需要修改对.overlap的调用,因为它显然没有导出:
if (!use.r.layout)
return(wordcloud:::.overlap(x1, y1, sw1, sh1, boxes))在循环完成后,我隐形地返回了它:
return(invisible(textmat[-1, ])) # to get rid of the NA row at the beginning将其命名为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"]) }如建议的那样:
with(tmat, grid.text(x=x1, y=y1, label=words, rot=rotWord,
gp=gpar( fontsize=9*cexw)) } # untested发布于 2013-03-23 17:39:38
你可以使用gridBase包,但是有一个巧妙的视窗。在这里,我使用vpStack来获得良好的尺寸。
par(mfrow=c(1, 2))
wordcloud(words, freqs)
plot.new()
vps <- baseViewports()
p <- qplot(1:10, rnorm(10), colour = runif(10))
print(p,vp = vpStack(vps$figure,vps$plot))

编辑如果你只想生成一个pdf,请使用knitr。
另一种选择是,如果你只想创建一个pdf,你可以使用Knitr。混合网格和基础图形非常简单。Latex可以帮你完成这项工作。例如,上面的结果可以通过这个块得到。
<<mixgridwithggplot, fig.show='hold',out.width='.5\\linewidth'>>=
wordcloud(words, freqs)
qplot(1:10, rnorm(10), colour = runif(10))
@我非常确定knitr可以在代码后面的单个png中创建2个图的png。
https://stackoverflow.com/questions/15584105
复制相似问题