2013-03-05 10 views
11

私は、用語 - 文書マトリックスからタグクラウドを生成する実用的なRコードを持っています。R:wordcloudグラフィック/ pngにタイトルを追加

今、多くのドキュメントから一群のタグクラウドを作成し、後で視覚的に検査したいと考えています。 タグクラウドの画像がどの文書/コーパスに属しているかを知るためには、生成された画像にタイトルを追加する必要があります。それ、どうやったら出来るの?

多分これは明らかですが、私はまだRグラフィックの初心者です。

私自身のコーパスは、ここでそれを一覧表示するにはあまりにも大きいですが、このSO質問(SOユーザーAndrieを使用することができますからコード形式と組み合わせる受け入れ答えからコード: Spaces in wordcloud 私はカスタムタイトルを追加したいとthis

答えて

14

wordcloud()機能プロット全体を塗りつぶします。つまり、プロットする前にタイトルのグラフィックスデバイスにスペースを確保する必要があります。

wordcloudは基本グラフを使用しているため、par(mfrow=...)またはlayout()のいずれかでこれを行うことができます。次に、プロットタイトルをtext()で作成します。

私は?wordcloudに例を適応させる、layout()を示しています

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") 

これが生成します。

enter image description here

4

ひとつのアイデアのような絵にいくつかのより多くのカスタムテキストは、イメージをインポートし、grid.rasterを使用してそれらを再度保存し、grid.textを使用してtitileを追加することです例:。

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

これはいいアイデアです、多分私は、余分な情報を追加する際に使用します後でPNGs。しかし、まさに私が望むものではありません。ワードクラウドの世代の時にタイトルを挿入してください。 – knb

+1

@kndあなたの質問(png)のタイトルは間違いにつながると思います。アンドリーはあなたに正しい答えを与えます。この答えはプロットの注釈のようなものです。 – agstudy

関連する問題