26. 詞云圖繪制
清除當(dāng)前環(huán)境中的變量
rm(list=ls())
設(shè)置工作目錄
setwd("C:/Users/Dell/Desktop/R_Plots/26wordcloud/")
使用wordcloud2包繪制詞云圖
# 安裝并加載所需R包
#install.packages("wordcloud2")
library(wordcloud2)
# 查看示例數(shù)據(jù)
# 第一列為詞語名,第二列為詞頻數(shù)
head(demoFreq)
## word freq
## oil oil 85
## said said 73
## prices prices 48
## opec opec 42
## mln mln 31
## the the 26
# 使用wordcloud2函數(shù)繪制詞云圖
# 默認繪圖
wordcloud2(demoFreq)

image.png
# 設(shè)置字體大小和寬度
wordcloud2(demoFreq, size = 2, fontWeight = "bold")

image.png
# shape參數(shù)設(shè)置詞云展現(xiàn)圖形
wordcloud2(demoFreq, size = 1,shape = 'star')

image.png
# 設(shè)置字體顏色和背景色
wordcloud2(demoFreq, size = 1.5,
color = "random-light",
backgroundColor = "grey")

image.png
# 設(shè)置字體旋轉(zhuǎn)的角度和旋轉(zhuǎn)比例,所有字體旋轉(zhuǎn)45°,一半字體旋轉(zhuǎn)
wordcloud2(demoFreq, size = 2,
minRotation = -pi/4, maxRotation = -pi/4,
rotateRatio = 0.5)

image.png
# 根據(jù)指定條件(詞頻大小)設(shè)置字體顏色
wordcloud2(demoFreq, size = 1.5,
color = ifelse(demoFreq[, 2] > 20, 'red', 'skyblue'))

image.png
# 自定義詞云展現(xiàn)圖形
figPath = system.file("examples/t.png",package = "wordcloud2")
wordcloud2(demoFreq, figPath = figPath,
size = 1.5,color = "skyblue")

image.png
# 使用letterCloud函數(shù)繪制詞云圖
# word參數(shù)指定詞云的形狀
letterCloud(demoFreq, word = "R")

image.png
letterCloud(demoFreq, word = "WORDCLOUD2", wordSize = 1)

image.png
使用wordcloud2包繪制詞云圖
# 安裝并加載所需R包
#install.packages("ggwordcloud")
library(ggwordcloud)
# 查看內(nèi)置數(shù)據(jù)集
data("love_words_small")
head(love_words_small)
## # A tibble: 6 x 4
## lang word native_speakers speakers
## <chr> <chr> <dbl> <dbl>
## 1 zh 愛 1200 1200
## 2 en Love 400 800
## 3 es Amor 480 555
## 4 ar <U+062D><U+0628> 245 515
## 5 hi <U+092A><U+094D><U+092F><U+093E><U+0930> 322 442
## 6 fr Amour 76.8 351.
# 使用geom_text_wordcloud函數(shù)繪制詞云圖
ggplot(love_words_small, aes(label = word, size = speakers)) +
geom_text_wordcloud(color = factor(sample.int(10, nrow(love_words_small), replace = TRUE))) +
scale_size_area(max_size = 20) +
theme_minimal()

image.png
# 使用geom_text_wordcloud_ares函數(shù)繪制詞云圖
ggplot(love_words_small, aes(label = word, size = speakers, color = speakers)) +
geom_text_wordcloud_area(shape = "star") +
scale_size_area(max_size = 20) +
theme_minimal() +
scale_color_gradient(low = "blue",high = "red")

image.png
# 使用ggwordcloud函數(shù)繪制詞云圖
ggwordcloud(words = love_words_small$word,
freq = love_words_small$speakers,
min.freq = 3,
random.order = T)

image.png
# 使用ggwordcloud2函數(shù)繪制詞云圖
ggwordcloud2(love_words_small[,c("word", "speakers")],
color = "random-dark",
size = 2,
shape = "circle")

image.png
使用d3wordcloud包繪制詞云圖
# 安裝并加載所需R包
#devtools::install_github("jbkunst/d3wordcloud")
library(d3wordcloud)
# 構(gòu)建示例數(shù)據(jù)
words <- c("I", "love", "this", "package", "but", "I", "don't", "like", "use", "wordclouds")
freqs <- sample(seq(length(words)))
head(words)
## [1] "I" "love" "this" "package" "but" "I"
head(freqs)
## [1] 4 7 10 2 3 6
# 使用d3wordcloud函數(shù)繪制詞云圖
d3wordcloud(words, freqs)

image.png
# colors參數(shù)設(shè)置顏色
d3wordcloud(words, freqs, colors = "#FFAA00")

image.png
# fonts參數(shù)設(shè)置字體
d3wordcloud(words, freqs, font = "Erica One", padding = 5)

image.png
# 設(shè)置字體旋轉(zhuǎn)角度
d3wordcloud(words, freqs, rotate.min = -45, rotate.max = 45)

image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] d3wordcloud_0.1 ggwordcloud_0.5.0 ggplot2_3.2.0 wordcloud2_0.2.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 pillar_1.4.2 compiler_3.6.0 base64enc_0.1-3
## [5] tools_3.6.0 digest_0.6.20 jsonlite_1.6 evaluate_0.14
## [9] tibble_2.1.3 gtable_0.3.0 pkgconfig_2.0.2 png_0.1-7
## [13] rlang_0.4.7 cli_1.1.0 yaml_2.2.0 xfun_0.8
## [17] withr_2.1.2 stringr_1.4.0 dplyr_0.8.3 knitr_1.23
## [21] vctrs_0.3.2 htmlwidgets_1.3 grid_3.6.0 tidyselect_0.2.5
## [25] glue_1.3.1 R6_2.4.0 fansi_0.4.0 rmarkdown_1.13
## [29] purrr_0.3.2 magrittr_1.5 scales_1.0.0 htmltools_0.3.6
## [33] assertthat_0.2.1 colorspace_1.4-1 labeling_0.3 utf8_1.1.4
## [37] stringi_1.4.3 lazyeval_0.2.2 munsell_0.5.0 crayon_1.3.4