学习绘制词云
wordcloud2 r包很好用!
05 Oct 2016
Go back一个新的R包,可以方便的画词云
下面是链接:
微信上的订阅号# install.packages("wordcloud")
# install.packages("wordcloud2")
library(wordcloud)
library(wordcloud2)
df<-demoFreq[1:100,]
# wordcloud(df$word, df$freq) 这边想要展示的是原来的词云的包比较丑
wordcloud2(df)
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)
data
A data frame including word and freq in each column
size
Font size, default is 1. The larger size means the bigger word.
minSize
A character string of the subtitle
gridSize
Size of the grid in pixels for marking the availability of the canvas the larger the grid size, the bigger the gap between words.
fontFamily
Font to use.
fontWeight
Font weight to use, e.g. normal, bold or 600
color
color of the text, keyword 'random-dark' and 'random-light' can be used. color vector is also supported in this param
backgroundColor
Color of the background.
minRotation
If the word should rotate, the minimum rotation (in rad) the text should rotate.
maxRotation
If the word should rotate, the maximum rotation (in rad) the text should rotate. Set the two value equal to keep all text in one angle.
shuffle
Shuffle the points to draw so the result will be different each time for the same list and settings.
rotateRatio
Probability for the word to rotate. Set the number to 1 to always rotate.
shape
The shape of the "cloud" to draw. Can be a keyword present. Available presents are 'circle' (default), 'cardioid' (apple or heart shape curve, the most known polar equation), 'diamond' (alias of square), 'triangle-forward', 'triangle', 'pentagon', and 'star'.
ellipticity
degree of "flatness" of the shape wordcloud2.js should draw.
widgetsize
size of the widgets
figPath
The path to a figure used as a mask.
hoverFunction
Callback to call when the cursor enters or leaves a region occupied by a word. A string of java script function.
wordcloud2(df)
wordcloud2(df, size = 2)
wordcloud2(df, size = 1,shape = 'pentagon')
wordcloud2(df, size = 1,shape = 'star')
wordcloud2(df, size = 2,
color = "random-light", backgroundColor = "grey")
wordcloud2(df, size = 2, minRotation = -pi/2, maxRotation = -pi/2)
wordcloud2(df, size = 2, minRotation = -pi/6, maxRotation = -pi/6,
rotateRatio = 1)
wordcloud2(df, size = 2, minRotation = -pi/6, maxRotation = pi/6,
rotateRatio = 0.9)
wordcloud2(demoFreqC[1:100,], size = 2,
color = "random-light", backgroundColor = "grey")
wordcloud2(demoFreqC[1:100,], size = 2, minRotation = -pi/6, maxRotation = -pi/6,
rotateRatio = 1)
# Color Vector
colorVec = rep(c('red', 'skyblue'), length.out=nrow(df))
wordcloud2(demoFreq, color = colorVec, fontWeight = "bold")
wordcloud2(df,
color = ifelse(df[, 2] > 20, 'red', 'skyblue'))
由于使用了Rstudio出品的htmlwidgets框架,knitr和shiny也都支持。以下是wordcloud2包的一个基本介绍。基本的函数有两个:
1.wordcloud2: 提供基本的词云功能 2.letterCloud: 使用选定的词绘制词云(就像刚才那个wordcloud2的图片)
为了文章的流畅性,详细的使用参数我就不细说了,可以去看下我留在CRAN的文档。下面会给大家介绍三种模式,按需选择。