ggwordcloud

Authors

[Editor] Hu Zheng;

[Contributors]

The word cloud is to visualize the β€œkeywords” that appear frequently in the web text by forming a β€œkeyword cloud layer” or β€œkeyword rendering”.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: data.table; jsonlite; ggwordcloud

# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
  install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
  install.packages("jsonlite")
}
if (!requireNamespace("ggwordcloud", quietly = TRUE)) {
  install.packages("ggwordcloud")
}
if (!requireNamespace("curl", quietly = TRUE)) {
  install.packages("curl")
}
if (!requireNamespace("png", quietly = TRUE)) {
  install.packages("png")
}

# Load packages
library(data.table)
library(jsonlite)
library(ggwordcloud)
library(curl)
library(png)

Data Preparation

Load data nouns and noun frequencies.

# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/ggwordcloud/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
inmask <- "https://download.hiplot.cn/api/file/fetch/?path=public/demo/ggwordcloud/hearth.png"

# Convert data structure
col <- data[, 2]
data <- cbind(data, col)

# View data
head(data)
    word freq col
1    oil   85  85
2   said   73  73
3 prices   48  48
4   opec   42  42
5    mln   31  31
6    the   26  26

Visualization

# ggwordcloud
p <- ggplot(data, aes(label = word, size = freq, color = col)) +
  scale_size_area(max_size = 40) +
  theme_minimal() + 
  geom_text_wordcloud_area(
    mask = png::readPNG(curl::curl_fetch_memory(inmask)$content), 
    rm_outside = TRUE) +
  scale_color_gradient(low = "#8B0000", high = "#FF0000")

p
FigureΒ 1: ggwordcloud

Display the proportion of nouns in the word cloud graph according to the frequency of nouns.