### ggwordcloud2 usage example Source: https://lepennec.github.io/ggwordcloud/reference/ggwordcloud2.html Example that sets a seed, loads the love_words_latin_small dataset, and creates a wordcloud using the word and speakers columns. ```r set.seed(42) data("love_words_latin_small") ggwordcloud2(love_words_latin_small[,c("word", "speakers")]) ``` -------------------------------- ### Install ggwordcloud from CRAN Source: https://lepennec.github.io/ggwordcloud/index.html Install the released version of ggwordcloud from CRAN. ```r install.packages("ggwordcloud") ``` -------------------------------- ### Install development version from GitHub Source: https://lepennec.github.io/ggwordcloud/index.html Install the development version of ggwordcloud from GitHub. Check the latest development version before submitting an issue. ```r devtools::install_github("lepennec/ggwordcloud") ``` -------------------------------- ### Word cloud examples with geom_text_wordcloud and geom_text_wordcloud_area Source: https://lepennec.github.io/ggwordcloud/reference/geom_text_wordcloud.html Two examples from the Examples section. The first uses geom_text_wordcloud() with a seed and the love_words_latin_small dataset. The second uses geom_text_wordcloud_area() without resetting the seed, continuing the same workflow. ```r set.seed(42) data("love_words_latin_small") ggplot(love_words_latin_small, aes(label = word, size = speakers)) + geom_text_wordcloud() + scale_size_area(max_size = 20) + theme_minimal() ``` ```r ggplot(love_words_latin_small, aes(label = word, size = speakers)) + geom_text_wordcloud_area() + scale_size_area(max_size = 20) + theme_minimal() ``` -------------------------------- ### Source file notice template (GPL v3.0) Source: https://lepennec.github.io/ggwordcloud/LICENSE.html Attach this notice to the start of each source file to state the exclusion of warranty and provide a pointer to the full license. Replace the placeholder line with the program's name and a brief description, and update the copyright line. ```plaintext Copyright (C) 2018 Erwan LE PENNEC This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ``` -------------------------------- ### Interactive mode notice (GPL v3.0) Source: https://lepennec.github.io/ggwordcloud/LICENSE.html If the program does terminal interaction, output this notice when it starts in interactive mode. The hypothetical commands 'show w' and 'show c' should display the appropriate parts of the General Public License. ```plaintext ggwordcloud Copyright (C) 2018 Erwan LE PENNEC This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. This is free software, and you are welcome to redistribute it under certain conditions; type 'show c' for details. ``` -------------------------------- ### Set original label positions with x aesthetic Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Specifies an original position for each label using the x aesthetic, which serves as the starting point for the spiral algorithm. The x axis is hidden with scale_x_discrete(breaks = NULL). ```r set.seed(42) ggplot( love_words_small_l, aes( label = word, size = speakers, x = type, color = type ) ) + geom_text_wordcloud_area() + scale_size_area(max_size = 30) + scale_x_discrete(breaks = NULL) + theme_minimal() ``` -------------------------------- ### Rotate words with arbitrary angles (45-degree increments) Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Rotate words with arbitrary angles, not just 90 degrees. This example assigns angles from -90 to 90 degrees (multiples of 45) with a weighted probability favoring 0 degrees. ```r love_words_small <- love_words_small %>% mutate(angle = 45 * sample(-2:2, n(), replace = TRUE, prob = c(1, 1, 4, 1, 1))) ``` ```r set.seed(42) ggplot(love_words_small, aes( label = word, size = speakers, angle = angle )) + geom_text_wordcloud_area() + scale_size_area(max_size = 40) + theme_minimal() ``` -------------------------------- ### Rotate words with angle aesthetic (90 degrees, 40%) Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Rotate words by setting the angle aesthetic. This example assigns a 90-degree rotation to a random 40% of the words using dplyr's mutate and sample, then plots with the angle aesthetic. ```r library(dplyr, quietly = TRUE) #> #> Attaching package: 'dplyr' #> The following objects are masked from 'package:stats': #> #> filter, lag #> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union ``` ```r love_words_small <- love_words_small %>% mutate(angle = 90 * sample(c(0, 1), n(), replace = TRUE, prob = c(60, 40))) ``` ```r set.seed(42) ggplot(love_words_small, aes( label = word, size = speakers, angle = angle )) + geom_text_wordcloud_area() + scale_size_area(max_size = 40) + theme_minimal() ``` -------------------------------- ### Basic word cloud with geom_text_wordcloud Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Creates a basic word cloud using the geom_text_wordcloud geom. The label aesthetic supplies the words, and theme_minimal() removes extra plot elements. The placement is random, so different seeds produce different layouts. ```r set.seed(42) ggplot(love_words_small, aes(label = word)) + geom_text_wordcloud() + theme_minimal() ``` -------------------------------- ### Word cloud with mask and color gradient Source: https://lepennec.github.io/ggwordcloud/index.html Create a word cloud with a mask, random angles, and color gradient. The output warns that some words could not fit on the page and were removed. ```r data("love_words") set.seed(42) ggplot( love_words, aes( label = word, size = speakers, color = speakers ) ) + geom_text_wordcloud_area(aes(angle = 45 * sample(-2:2, nrow(love_words), replace = TRUE, prob = c(1, 1, 4, 1, 1) )), mask = png::readPNG(system.file("extdata/hearth.png", package = "ggwordcloud", mustWork = TRUE )), rm_outside = TRUE ) + scale_size_area(max_size = 40) + theme_minimal() + scale_color_gradient(low = "darkred", high = "red") #> Some words could not fit on page. They have been removed. ``` -------------------------------- ### Load love words datasets Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Loads the built-in datasets love_words_small and love_words. love_words_small contains languages with more than 50 million speakers; love_words contains many more languages. ```r data("love_words_small") data("love_words") ``` -------------------------------- ### Basic word cloud with geom_text_wordcloud Source: https://lepennec.github.io/ggwordcloud/index.html Create a basic word cloud using geom_text_wordcloud with the love_words_small dataset. The output includes a message that ggplot2 is required. ```r library(ggwordcloud) #> Loading required package: ggplot2 data("love_words_small") set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud() + scale_size_area(max_size = 40) + theme_minimal() ``` -------------------------------- ### Facet word clouds by type using gather and facet_wrap Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Prepares data for faceting by gathering speaker and native speaker counts into a long format, then creates two word clouds with facet_wrap(~type). The first block loads dplyr and tidyr and reshapes the data; the second plots with the same scales. ```r library(dplyr, quietly = TRUE, warn.conflicts = FALSE) library(tidyr, quietly = TRUE) love_words_small_l <- love_words_small %>% gather(key = "type", value = "speakers", -name, -word, -angle, -iso_639_3) %>% arrange(desc(speakers)) ``` ```r set.seed(42) ggplot( love_words_small_l, aes(label = word, size = speakers) ) + geom_text_wordcloud_area() + scale_size_area(max_size = 30) + theme_minimal() + facet_wrap(~type) ``` -------------------------------- ### Create a signed power transform with power_trans() Source: https://lepennec.github.io/ggwordcloud/reference/power_trans.html Creates a signed power transform with the given exponent. The default power is 1, which corresponds to the identity transform. ```r power_trans(power = 1) ``` -------------------------------- ### Word cloud with custom label content using HTML Source: https://lepennec.github.io/ggwordcloud/index.html Create a word cloud with custom label content using HTML markup in the label_content aesthetic. The label includes the word and the speakers value in a smaller font. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers, label_content = sprintf("%s(%g)", word, speakers))) + geom_text_wordcloud_area() + scale_size_area(max_size = 40) + theme_minimal() ``` -------------------------------- ### Load ggwordcloud package Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Loads the ggwordcloud package, which also loads ggplot2 as a dependency. ```r library(ggwordcloud) #> Loading required package: ggplot2 ``` -------------------------------- ### Approximate wordcloud replacement with ggwordcloud Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Approximate replacement for the wordcloud package. Uses a similar syntax and produces a word cloud with font size proportional to the raw size aesthetic (not its square root). ```r set.seed(42) ggwordcloud(love_words_small$word, love_words_small$speakers) ``` -------------------------------- ### Approximate wordcloud2 replacement with ggwordcloud2 Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Approximate replacement for the wordcloud2 package. Takes a data frame with word and speakers columns and a size parameter; font size is proportional to the raw size aesthetic. ```r set.seed(42) ggwordcloud2(love_words_small[, c("word", "speakers")], size = 2.5) ``` -------------------------------- ### Masked word cloud with png::readPNG and rm_outside Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Uses a mask image (hearth.png from the ggwordcloud package) to restrict word placement to non-transparent pixels, with rm_outside = TRUE to remove words outside the mask. The displayed warning indicates that some words could not fit and were removed. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud_area( mask = png::readPNG(system.file("extdata/hearth.png", package = "ggwordcloud", mustWork = TRUE )), rm_outside = TRUE ) + scale_size_area(max_size = 42) + theme_minimal() #> Warning in wordcloud_boxes(data_points = points_valid_first, boxes = boxes, : #> Some words could not fit on page. They have been removed. ``` -------------------------------- ### geom_text_wordcloud function signature Source: https://lepennec.github.io/ggwordcloud/reference/geom_text_wordcloud.html Shows the complete argument list for geom_text_wordcloud, including mapping, data, stat, position, parse, nudge_x, nudge_y, eccentricity, rstep, tstep, perc_step, max_steps, grid_size, max_grid_size, grid_margin, xlim, ylim, seed, rm_outside, shape, mask, area_corr, na.rm, show.legend, inherit.aes, show_boxes, and use_richtext. The default for use_richtext is TRUE. ```r geom_text_wordcloud( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, eccentricity = 0.65, rstep = 0.01, tstep = 0.02, perc_step = 0.01, max_steps = 10, grid_size = 4, max_grid_size = 128, grid_margin = 1, xlim = c(NA, NA), ylim = c(NA, NA), seed = NA, rm_outside = FALSE, shape = "circle", mask = NA, area_corr = FALSE, na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, show_boxes = FALSE, use_richtext = TRUE ) ``` -------------------------------- ### Word cloud with a different random seed Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Shows that changing the random seed (here 43 instead of 42) yields a different word cloud layout because the placement algorithm is random. ```r set.seed(43) ggplot(love_words_small, aes(label = word)) + geom_text_wordcloud() + theme_minimal() ``` -------------------------------- ### ggwordcloud2(data, size, color, minRotation, maxRotation, shuffle, rotateRatio, shape, ellipticity, figPath, ...) Source: https://lepennec.github.io/ggwordcloud/reference/ggwordcloud2.html Creates a word cloud plot as a ggplot object. It is an approximate replacement for wordcloud2, with similar syntax but fewer options, such as no background image support. ```APIDOC ## ggwordcloud2 ### Description Creates a word cloud plot as a ggplot object. It is an approximate replacement for wordcloud2, with similar syntax but fewer options, such as no background image support. ### Usage ```r ggwordcloud2( data, size = 1, color = "random-dark", minRotation = -pi/4, maxRotation = pi/4, shuffle = TRUE, rotateRatio = 0.4, shape = "circle", ellipticity = 0.65, figPath = NA, ... ) ``` ### Arguments - **data** (data.frame or table) - Required - A dataframe whose two first columns are the names and the freqs, or a table. - **size** (numeric) - Optional - Scaling factor. Default to 1. - **color** (character or list) - Optional - Color scheme either "random-dark", "random-light" or a list of colors of the size of the dataframe. Default to "random-dark". - **minRotation** (numeric) - Optional - The minimal rotation angle. - **maxRotation** (numeric) - Optional - The maximal rotation angle. - **shuffle** (logical) - Optional - If TRUE, the words are shuffled at the beginning. - **rotateRatio** (numeric) - Optional - The proportion of rotated words. - **shape** (character) - Optional - Control the shape of the cloud. - **ellipticity** (numeric) - Optional - Control the eccentricity of the wordcloud. - **figPath** (character) - Optional - Path to an image used as a mask. - **...** - Optional - The remaining parameters are passed to geom_text_wordcloud. ### Value A ggplot object. ### Examples ```r set.seed(42) data("love_words_latin_small") ggwordcloud2(love_words_latin_small[,c("word", "speakers")]) ``` ``` -------------------------------- ### ggwordcloud Source: https://lepennec.github.io/ggwordcloud/reference/ggwordcloud.html Creates a word cloud plot from words and frequencies, returning a ggplot object. ```APIDOC ## ggwordcloud ### Description Creates a word cloud plot as an approximate replacement for the `wordcloud` package. It accepts words and their frequencies and returns a ggplot object. ### Method Function call ### Endpoint `ggwordcloud(words, freq, scale = c(4, 0.5), min.freq = 3, max.words = Inf, random.order = TRUE, random.color = FALSE, rot.per = 0.1, colors = "black", ordered.colors = FALSE, ...)` ### Parameters - **words** (character vector) - Required - The words to be plotted. - **freq** (numeric vector) - Required - Their frequencies. - **scale** (numeric vector of length 2) - Optional - Range of the size of the words. Default: `c(4, 0.5)`. - **min.freq** (numeric) - Optional - Words with frequency below this value will not be plotted. Default: `3`. - **max.words** (numeric) - Optional - Maximum number of words to be plotted; least frequent terms are dropped. Default: `Inf`. - **random.order** (logical) - Optional - If `TRUE`, words are plotted in random order; if `FALSE`, they are plotted in decreasing frequency. Default: `TRUE`. - **random.color** (logical) - Optional - If `TRUE`, colors are chosen randomly from the `colors` vector; if `FALSE`, color is chosen based on frequency. Default: `FALSE`. - **rot.per** (numeric) - Optional - Proportion of words with 90-degree rotation. Default: `0.1`. - **colors** (character vector) - Optional - Color words from least to most frequent. Default: `"black"`. - **ordered.colors** (logical) - Optional - If `TRUE`, colors are assigned to words in order. Default: `FALSE`. - **...** - Optional - Additional parameters to be passed to `geom_text_wordcloud`. ### Return Value A ggplot object. ### Example ```r set.seed(42) data("love_words_latin_small") ggwordcloud(love_words_latin_small$word, love_words_latin_small$speakers) ``` ``` -------------------------------- ### Continuous color gradient with scale_color_gradient in ggwordcloud Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Maps the color aesthetic to the speakers value and applies a gradient color scale from darkred to red. Uses geom_text_wordcloud_area with the angle column and a maximum size of 40. ```r set.seed(42) ggplot( love_words_small, aes( label = word, size = speakers, color = speakers, angle = angle ) ) + geom_text_wordcloud_area() + scale_size_area(max_size = 40) + theme_minimal() + scale_color_gradient(low = "darkred", high = "red") ``` -------------------------------- ### BibTeX citation entry Source: https://lepennec.github.io/ggwordcloud/authors.html BibTeX @Manual entry to cite the ggwordcloud package in academic writing. Include it in your LaTeX file's bibliography, using tools like knitr::write_bib() or bibentry() to manage the entry. ```bibtex @Manual{, title = {ggwordcloud: A Word Cloud Geom for 'ggplot2'}, author = {Erwan {Le Pennec} and Kamil Slowikowski}, year = {2024}, note = {R package version 0.6.2, https://lepennec.github.io/ggwordcloud/}, url = {https://github.com/lepennec/ggwordcloud}, } ``` -------------------------------- ### Loop over ggwordcloud shapes with geom_text_wordcloud_area Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Loops over the eight supported shapes (circle, cardioid, diamond, square, triangle-forward, triangle-upright, pentagon, star) and prints a word cloud for each using geom_text_wordcloud_area with the shape option. Sets the random seed to 42 for reproducibility and uses love_words_small with speakers as the size aesthetic. ```r for (shape in c( "circle", "cardioid", "diamond", "square", "triangle-forward", "triangle-upright", "pentagon", "star" )) { set.seed(42) print(ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud_area(shape = shape) + scale_size_area(max_size = 40) + theme_minimal() + ggtitle(shape)) } ``` -------------------------------- ### Create word cloud with angles and mask using geom_text_wordcloud_area Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Creates a word cloud with random angles and a custom mask image. The first block adds an angle column with weighted probabilities; the second uses geom_text_wordcloud_area with the mask and rm_outside = TRUE to remove words outside the mask. ```r love_words <- love_words %>% mutate(angle = 45 * sample(-2:2, n(), replace = TRUE, prob = c(1, 1, 4, 1, 1))) ``` ```r set.seed(42) ggplot( love_words, aes( label = word, size = speakers, color = speakers, angle = angle ) ) + geom_text_wordcloud_area( mask = png::readPNG(system.file("extdata/hearth.png", package = "ggwordcloud", mustWork = TRUE )), rm_outside = TRUE ) + scale_size_area(max_size = 40) + theme_minimal() + scale_color_gradient(low = "darkred", high = "red") ``` -------------------------------- ### Word cloud with geom_text_wordcloud_area Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Uses the geom_text_wordcloud_area geom as an equivalent to geom_text_wordcloud(area_corr = TRUE). By default, the text area is proportional to the raw size aesthetic. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud_area() + scale_size_area(max_size = 50) + theme_minimal() ``` -------------------------------- ### Word cloud with power_trans scale Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Applies a power_trans scale with a factor of 1/.7 to better match human area perception. This modifies the scaling of the text area in the word cloud. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud_area() + scale_size_area(max_size = 50, trans = power_trans(1/.7)) + theme_minimal() ``` -------------------------------- ### Word cloud with area_corr option Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Enables the area_corr option in geom_text_wordcloud to scale the font so that the text area is a function of the raw size aesthetic, when combined with scale_size_area(). ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud(area_corr = TRUE) + scale_size_area(max_size = 50) + theme_minimal() ``` -------------------------------- ### Random factor color mapping in ggwordcloud Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Assigns a random factor color to each word by sampling 10 integers with replacement and mapping it to the color aesthetic. Uses geom_text_wordcloud_area with the angle column and a maximum size of 40. ```r set.seed(42) ggplot( love_words_small, aes( label = word, size = speakers, color = factor(sample.int(10, nrow(love_words_small), replace = TRUE)), angle = angle ) ) + geom_text_wordcloud_area() + scale_size_area(max_size = 40) + theme_minimal() ``` -------------------------------- ### Default word cloud with unplaced words warning Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Default behavior when words cannot fit: they are placed at the center of the word cloud and a warning is issued. Use this to see the default placement and warning message. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud_area() + scale_size_area(max_size = 80) + theme_minimal() #> Warning in wordcloud_boxes(data_points = points_valid_first, boxes = boxes, : #> Some words could not fit on page. They have been placed at their original #> positions. ``` -------------------------------- ### ggwordcloud2 function signature Source: https://lepennec.github.io/ggwordcloud/reference/ggwordcloud2.html Shows the full argument list for ggwordcloud2, including defaults and the ... parameter passed to geom_text_wordcloud. ```r ggwordcloud2( data, size = 1, color = "random-dark", minRotation = -pi/4, maxRotation = pi/4, shuffle = TRUE, rotateRatio = 0.4, shape = "circle", ellipticity = 0.65, figPath = NA, ... ) ``` -------------------------------- ### Use label_content aesthetic to modify label text Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Uses the label_content aesthetic to display a different label than the one used for sizing. The label shows the word followed by the speaker count in parentheses. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers, label_content = sprintf("%s (%g)", word, speakers))) + geom_text_wordcloud_area() + scale_size_area(max_size = 30) + theme_minimal() ``` -------------------------------- ### Word cloud with scale_radius for linear scaling Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Uses scale_radius() to obtain linear scaling between the value and the font size, matching the default behavior of wordcloud and wordcloud2. The range and limits arguments control the font size range. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud() + scale_radius(range = c(0, 30), limits = c(0, NA)) + theme_minimal() ``` -------------------------------- ### Word cloud with scale_size_area Source: https://lepennec.github.io/ggwordcloud/articles/ggwordcloud.html Uses scale_size_area() to make the font size proportional to the value (true proportionality) and to control the maximum font size. This overrides the default square root scaling. ```r set.seed(42) ggplot(love_words_small, aes(label = word, size = speakers)) + geom_text_wordcloud() + scale_size_area(max_size = 30) + theme_minimal() ``` -------------------------------- ### Faceted word cloud with geom_text_wordcloud_area Source: https://lepennec.github.io/ggwordcloud/index.html Create a faceted word cloud using geom_text_wordcloud_area and facet_wrap. Requires dplyr and tidyr for data manipulation. ```r library(dplyr, quietly = TRUE, warn.conflicts = FALSE) library(tidyr, quietly = TRUE) set.seed(42) ggplot( love_words_small %>% gather(key = "type", value = "speakers", -lang, -word) %>% arrange(desc(speakers)), aes(label = word, size = speakers) ) + geom_text_wordcloud_area() + scale_size_area(max_size = 40) + theme_minimal() + facet_wrap(~type) ```