-->

R:添加标题wordcloud图形/ PNG(R: add title to wordcloud g

2019-08-17 07:26发布

我有产生从术语的文档矩阵的标记云一些工作R代码里面。

现在我想创建许多文件标签云的一大堆,以及他们在以后的时间目视检查。 要知道标签云图片属于哪个文件(S)/语料库,我希望不要去想一个标题添加到生成的图形。 我怎么做?

也许这是显而易见的,但我仍然与R图形初学者。

我自己的语料库是太大,在这里列出来,但是从这个SO问题(结合的代码的代码,因此可以使用用户Andrie形成公认的答案: 在wordcloud空间我想添加自定义标题和一些自定义文本像照片这样

Answer 1:

wordcloud()函数填充了整个剧情。 这意味着你需要在绘制之前,你的显卡设备上预留的空间称号。

由于wordcloud利用基地grapics的,你可以做这一点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")

这会产生:



Answer 2:

一个想法是要导入的图片,并再次使用保存grid.raster ,并使用添加titile 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()

})


文章来源: R: add title to wordcloud graphics / png