R语言可视化(二十六):词云图绘制

26. 词云图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/26wordcloud/")

使用wordcloud2包绘制词云图

# 安装并加载所需R包
#install.packages("wordcloud2")
library(wordcloud2)

# 查看示例数据
# 第一列为词语名,第二列为词频数
head(demoFreq)
##          word freq
## oil       oil   85
## said     said   73
## prices prices   48
## opec     opec   42
## mln       mln   31
## the       the   26

# 使用wordcloud2函数绘制词云图
# 默认绘图
wordcloud2(demoFreq)
image.png
# 设置字体大小和宽度
wordcloud2(demoFreq, size = 2, fontWeight = "bold")
image.png
# shape参数设置词云展现图形
wordcloud2(demoFreq, size = 1,shape = 'star')
image.png
# 设置字体颜色和背景色
wordcloud2(demoFreq, size = 1.5,
           color = "random-light", 
           backgroundColor = "grey")
image.png
# 设置字体旋转的角度和旋转比例,所有字体旋转45°,一半字体旋转
wordcloud2(demoFreq, size = 2, 
           minRotation = -pi/4, maxRotation = -pi/4,
           rotateRatio = 0.5)
image.png
# 根据指定条件(词频大小)设置字体颜色
wordcloud2(demoFreq, size = 1.5,
           color = ifelse(demoFreq[, 2] > 20, 'red', 'skyblue'))
image.png
# 自定义词云展现图形
figPath = system.file("examples/t.png",package = "wordcloud2")
wordcloud2(demoFreq, figPath = figPath, 
           size = 1.5,color = "skyblue")
image.png
# 使用letterCloud函数绘制词云图
# word参数指定词云的形状
letterCloud(demoFreq, word = "R")
image.png
letterCloud(demoFreq, word = "WORDCLOUD2", wordSize = 1)
image.png

使用wordcloud2包绘制词云图

# 安装并加载所需R包
#install.packages("ggwordcloud")
library(ggwordcloud)

# 查看内置数据集
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函数绘制词云图
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函数绘制词云图
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函数绘制词云图
ggwordcloud(words = love_words_small$word, 
            freq = love_words_small$speakers,
            min.freq = 3,
            random.order = T)
image.png
# 使用ggwordcloud2函数绘制词云图
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)

# 构建示例数据
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函数绘制词云图
d3wordcloud(words, freqs)
image.png
# colors参数设置颜色
d3wordcloud(words, freqs, colors = "#FFAA00")
image.png
# fonts参数设置字体
d3wordcloud(words, freqs, font = "Erica One", padding = 5)
image.png
# 设置字体旋转角度
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