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