R: add title to wordcloud graphics / png

后端 未结 2 1722
渐次进展
渐次进展 2021-02-13 16:22

I have some working R code that generates a tag cloud from a term-document matrix.

Now I want to create a whole bunch of tag clouds from many documents, and to inspect

相关标签:
2条回答
  • 2021-02-13 17:05

    The wordcloud() function fills the entire plot. That means you need to reserve space on your graphics device for the title before plotting.

    Since wordcloud make use of base grapics, you can do this with either par(mfrow=...) or layout(). Then create the plot title with text().

    I illustrate with layout(), adapting the example in ?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")
    

    This generates:

    enter image description here

    0 讨论(0)
  • 2021-02-13 17:19

    One idea is to import the images , and save again them using grid.raster, and add the titile using grid.text. For example:

    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()
    
    })
    
    0 讨论(0)
提交回复
热议问题