如何在grob中放置wordcloud? [英] How to put a wordcloud in a grob?
问题描述
我创建了一个简单的wordcloud:
require(wordcloud)
words< - c('富有创意''愤世嫉俗''胆大''可靠''随和''精力充沛''有爱','有抱负','焦虑' ,有趣的,慷慨的,真诚的,善良的听众,善良的猎手,高兴的,勤劳的,无情的,冲动的,聪明的,善良的,忠诚的乐观','外向','离谱','激情','感知','身体健康','安静','理性','尊重','浪漫','害羞' ,'自发的','甜的','体贴的','温暖的')
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,f要求)
我想把它放到一个grob中,以便我可以安排几个在 gridExtra
包中使用 grid.arrange()
的其他图
require(ggplot2)
p1 < - qplot(1:10,rnorm(10),color = runif(10))
require(gridExtra )
grid.arrange(p1,my.wordcloud)
我知道我的wordcloud必须做一个grob来做到这一点,但我不明白如何做到这一点。我尝试使用 GROB在
功能,但这并没有工作。建议? gridExtra
包()
在 wordcloud $ c中修改代码并不困难$ c>来构造数据需要在网格中填充一个text.grob。在限制为0的窗口之后,
wordcloud
代码将x,y,text和rot值发送到基础 text
。,0和1,1指定
我所需要的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,
textmat [i,3]< - words [i]
需要修改对 .overlap
的调用,因为它显然不会被导出:
if(!use.r.layout)
return(wordcloud :::。overlap(x1,y1,sw1,sh1,boxes))
我在循环完成后不可见地将其返回:
收益率(不可见(textma吨[-1,))#到开头
摆脱NA列的命名后it wordcloud2:
> tmat< - wordcloud2(c(letters,LETTERS,0:9),seq(1,1000,len = 62))
> str(tmat)
'data.frame':61 obs。 5个变量:
$ x1:num 0.493 0.531 0.538 0.487 ...
$ y1:num 0.497 0.479 0.532 0.475 ...
$ words:chrbOM ...
$ 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))}#未经测试
I've created a simple 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)
I would like to put this into a "grob" so that I can arrange it with several other plots using grid.arrange()
in the gridExtra
package:
require(ggplot2)
p1 <- qplot(1:10, rnorm(10), colour = runif(10))
require(gridExtra)
grid.arrange(p1, my.wordcloud)
I understand that my wordcloud must be a "grob" to do this, but I don't understand how to make this so. I tried using the grob()
function in the gridExtra
package, but this didn't work. Suggestions?
It shouldn't be that difficult to adapt the code in wordcloud
to construct the data need to fill in a text.grob in grid. The wordcloud
code sends x, y, text and rot values to the base text
function after a window with limits of 0,0 and 1, 1 is specified.
I needed to add this before the for-loop:
textmat <- data.frame(x1=rep(NA, length(words)), y1=NA, words=NA_character_,
rotWord=NA, cexw=NA, stringsAsFactors=FALSE )
This at the end of the for-loop:
textmat[i, c(1,2,4,5) ] <- c(x1=x1, y1=y1, rotWord=rotWord*90, cexw = size[i] )
textmat[i, 3] <- words[i]
And needed to amend the call to .overlap
, because it is apparently not exported:
if (!use.r.layout)
return(wordcloud:::.overlap(x1, y1, sw1, sh1, boxes))
And I returned it invisibly after the loop was complete:
return(invisible(textmat[-1, ])) # to get rid of the NA row at the beginning
After naming it 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"]) }
As suggested:
with(tmat, grid.text(x=x1, y=y1, label=words, rot=rotWord,
gp=gpar( fontsize=9*cexw)) } # untested
这篇关于如何在grob中放置wordcloud?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!