15

我有一些工作 R 代码可以从术语文档矩阵生成标签云。

现在我想从许多文档中创建一大堆标签云,并在以后直观地检查它们。要知道标签云图片属于哪个文档/语料库,我想为生成的图形添加一个标题。我怎么做?

也许这很明显,但我仍然是 R 图形的初学者。

我自己的语料库太大,无法在此处列出,但此 SO 问题的代码(结合 SO 用户 Andrie 接受的答案的代码形式可以使用: Spaces in wordcloud I want to add a custom title and some more custom text像这样的图片

4

2 回答 2

24

wordcloud()函数填充整个绘图。这意味着您需要在绘图之前在图形设备上为标题预留空间。

由于wordcloud使用基本图形,因此您可以使用par(mfrow=...)或来执行此操作layout()。然后用 . 创建情节标题text()

我用 来说明layout(),将示例改编为?wordcloud

library(tm)
library(wordcloud)

x <- "Many years ago the great British explorer George Mallory, who 
was to die on Mount Everest, was asked why did he want to climb 
it. He said, \"Because it is there.\"

Well, space is there, and we're going to climb it, and the 
moon and the planets are there, and new hopes for knowledge 
and peace are there. And, therefore, as we set sail we ask 
God's blessing on the most hazardous and dangerous and greatest 
adventure on which man has ever embarked."

layout(matrix(c(1, 2), nrow=2), heights=c(1, 4))
par(mar=rep(0, 4))
plot.new()
text(x=0.5, y=0.5, "Title of my first plot")
wordcloud(x, main="Title")

这会产生:

在此处输入图像描述

于 2013-03-05T14:46:00.503 回答
4

一种想法是导入图像,然后使用 再次保存它们grid.raster,并使用 添加标题grid.text。例如:

ll <- list.files(patt='*.png')
library(png)
library(grid)
imgs <- lapply(ll,function(x){
  img <- as.raster(readPNG(x))
  ## get the file name
  x.name <- gsub('(.*).png','\\1',x)
  ## new device for new image version
  png(file =paste(x.name,'_modified','.png',sep=''))
  grid.raster(img)
  ## here I add title
  grid.text(label = x.name,x=0.5,y=0.9,gp=gpar(cex=2))
  dev.off()

})
于 2013-03-05T13:50:49.023 回答