Sunday, September 15, 2024

Word Cloud using R

Word Cloud Using R

Introduction

In this blog, we will look at how to quickly create a word cloud.

Step 1: Importing the libraries

package.name<-c("tidyverse","wordcloud")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}
Loading required package: tidyverse
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Loading required package: wordcloud

Loading required package: RColorBrewer


Step 2:Creating sample data


df<-data.frame(word = c("Linear","Logistic","Random Foreast",
                        "HYper Parameter Tuning","Confusion Matrix","XG-Boost",
                        "Neural Network","ANN","RNN","CNN"),
               freq=round(rnorm(10,50,10),0))
df
                     word freq
1                  Linear   45
2                Logistic   52
3          Random Foreast   45
4  HYper Parameter Tuning   62
5        Confusion Matrix   52
6                XG-Boost   44
7          Neural Network   32
8                     ANN   44
9                     RNN   70
10                    CNN   44


Step 3: Creating the plot

par(bg="white")

wordcloud(df[["word"]],
          df[["freq"]])
Warning in wordcloud(df[["word"]], df[["freq"]]): HYper Parameter Tuning could
not be fit on page. It will not be plotted.

Step 4: Adding colors

par(bg="white")

wordcloud(df[["word"]],
          df[["freq"]],
          colors = brewer.pal(8,"Dark2"))
Warning in wordcloud(df[["word"]], df[["freq"]], colors = brewer.pal(8, : HYper
Parameter Tuning could not be fit on page. It will not be plotted.


Step 5: Set minimum and maximum scale

par(bg="white")

wordcloud(df[["word"]],
          df[["freq"]],
          colors = brewer.pal(8,"Dark2"),
          scale = c(4,0.3),
          use.r.layout = F)# this is for collision detection
Warning in wordcloud(df[["word"]], df[["freq"]], colors = brewer.pal(8, : HYper
Parameter Tuning could not be fit on page. It will not be plotted.


Step 6: Add more rotating words

using rot.per argument.It gives proportion of words with 90 degree rotation


random.order will plot the words randomly otherwise they are plotted in decreasing order of frequency.

par(bg="white")

wordcloud(df[["word"]],
          df[["freq"]],
          colors = brewer.pal(8,"Dark2"),
          scale = c(4,0.3),
          use.r.layout = F,
          rot.per = 0.3,
          random.order = T)
Warning in wordcloud(df[["word"]], df[["freq"]], colors = brewer.pal(8, : HYper
Parameter Tuning could not be fit on page. It will not be plotted.

There are other arguments also such as max.words, ordered.colors, etc which can be used to create more customization.

No comments:

Post a Comment

Word Cloud using R

Word Cloud Using R Word Cloud Using R 2024-09-16 Introduction I...