R言語ワードクラウドアーティファクトwordcloud2パッケージ



R Language Word Cloud Artifact Wordcloud2 Package



前回の記事に続き、wordcloud2パッケージを要約すると、主な関数にはwordcloud2()とletterword()があり、wordcloud2関数を使用してワードクラウドを生成します。letterwordはワードクラウドをテキストの形に設定し、これら2つの関数を個別に紹介します。

  • Wordcloud2関数
wordcloud2(data, size = 1, minSize = 0, gridSize = 0, fontFamily = 'Segoe UI', fontWeight = 'bold', color = 'random-dark', backgroundColor = 'white', minRotation = -pi/4, maxRotation = pi/4, shuffle = TRUE, rotateRatio = 0.4, shape = 'circle', ellipticity = 0.65, widgetsize = NULL, figPath = NULL, hoverFunction = NULL)

主なパラメータが紹介されています:



(1) data: A data frame including word and freq in each column (2) size:Font size, default is 1. The larger size means the bigger word. (3) fontFamily: font, such as 'Aril' (4) fontWeight: font thickness, such as normal, bold, 600 (5) color: font color, you can choose 'random-dark' or 'random-light' (6) backgroundColor:Color of the background. (7) minRotation and maxRotation: the minimum and maximum values ​​of the text rotation angle (8) rotateRatio: font rotation ratio, if set to 1, all words will rotate (9) shape: the shape of the word cloud, optionally: 'circle' (circle), 'star' (five-pointed star), 'cardioid' (apple or heart), 'diamond' (diamond), 'triangle- Forward' (triangle_lying), 'triangle' (triangle_upright), 'pentagon' (pentagon) (10) figPath:The path to a figure used as a mask.
  • Lettercloud関数
letterCloud(data, word, wordSize = 0, letterFont = NULL, ...)

以下では、関数の特定の使用法を紹介する例として、パッケージに付属のデータセットdemoFreqCを取り上げます。

library(wordcloud2) wordcloud2(demoFreqC, size = 1, shape = 'circle', fontFamily = 'Microsoft Yahei', fontWeight = 'nornal', rotateRatio = 0.5, color = 'random-light', backgroundColor = 'snow')

円形のワードクラウドを生成します。フォントはMicrosoftYahei、テキストの回転率は0.5、明るい色、デスクトップの背景色は雪、効果は次のとおりです。
画像
サイズを2に設定するなど、必要なグラフィックを取得するためにパラメーターを引き続き調整して、ワードクラウドがキャンバス全体に広がるようにすることができます。
画像
さらに、生成されたワードクラウドページは、動的ラベルを表示するためにマウスをサポートします。つまり、マウスを「データ」などの単語に置くと、その単語が表示される回数が表示されます。
組み込みの形状を持つワードクラウドを生成することに加えて、wordcloud2はカスタム画像形状もサポートし、白い背景画像をダウンロードし、figPathパラメーターを使用してこの画像のパスを呼び出して画像を描画します。下のツイートアイコンの形のような画像の形のワードクラウド
画像



wordcloud2(demoFreqC, size = 1, fontFamily = 'Microsoft Yahei', fontWeight = '600', rotateRatio = 1, color = 'random-dark', backgroundColor = 'snow', figPath = '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/wordcloud2/examples/t.png' )

生成されるワードクラウドは次のとおりです。
画像
画像の組み込み形状とカスタム形状に加えて、lettercloud関数を使用して、R字型のワードクラウドなどのカスタムテキスト形状を生成することもできます。

letterCloud(demoFreqC, 'R', wordSize = 0.3)

効果は次のとおりです。
画像
中国語を生成する場合は、letterFontを使用して、使用する中国語フォントを指定する必要があります。これには、使用する前に、コンピューターに使用されている中国語フォントを知っておく必要があります。 Macはフォントリストで見ることができます。フォントが指定されていない場合、中国語は文字化けし、すべての単語は「口」に似たボックスを生成します。

letterCloud(demoFreqC, 'Analysis', wordSize = 0.3, letterFont = 'Hei')

効果は次のとおりです。
画像
概要:wordcloud2は依然として非常に機能的で、パッケージに付属する一元化されたシェイプシェイプに加えて、多くのシェイプワードクラウドを生成できます。また、画像を使用して任意のイメージシェイプを生成し、lettercloudで任意のテキストシェイプを生成できます。