Other word clouds and word networks

Text Mining with Bag-of-Words in R

Ted Kwartler

Instructor

Commonality clouds

 

commonality.png

Text Mining with Bag-of-Words in R

Commonality clouds

# Combine both corpora: all_tweets
all_coffee <- paste(coffee_tweets$text,
                    collapse = "")
all_chardonnay <- paste(chardonnay_tweets$text,
                        collapse = "")

all_tweets <- c(all_coffee, all_chardonnay)
# Clean all_tweets all_tweets <- VectorSource(all_tweets) all_corpus <- VCorpus(all_tweets) all_clean <- clean_corpus(all_corpus) all_dm <- TermDocumentMatrix(all_clean) all_m <- as.matrix(all_tdm)
# Make commonality cloud commonality.cloud(all_m, colors = "steelblue1", max.words = 100)

commonality_cloud.png

Text Mining with Bag-of-Words in R

Comparison clouds

 

comparison.png

Text Mining with Bag-of-Words in R

Comparison clouds

# Combine both corpora: all_tweets
all_coffee <- paste(coffee_tweets$text, 
                    collapse = "")
all_chardonnay <- paste(chardonnay_tweets$text, 
                       collapse = "")
all_tweets <- c(all_coffee, all_chardonnay)

# Clean all_tweets all_tweets <- VectorSource(all_tweets) all_corpus <- VCorpus(all_tweets) all_clean <- clean_corpus(all_corpus) all_tdm <- TermDocumentMatrix(all_clean)
colnames(all_tdm) <- c("coffee", "chardonnay")
all_m <- as.matrix(all_tdm) # Make comparison cloud comparison.cloud(all_m, colors = c("orange", "blue"), max.words = 50)

comparison cloud

Text Mining with Bag-of-Words in R

Pyramid plots

# Identify terms shared by both documents
common_words <- subset(
  all_tdm_m,
  all_tdm_m[, 1] > 0 & all_tdm_m[, 2] > 0
)

# Find most commonly shared words difference <- abs(common_words[, 1] - common_words[, 2])
common_words <- cbind(common_words, difference) common_words <- common_words[order(common_words[, 3], decreasing = TRUE), ] top25_df <- data.frame(x = common_words[1:25, 1], y = common_words[1:25, 2], labels = rownames(common_words[1:25, ]))
Text Mining with Bag-of-Words in R

Pyramid plots

# Make pyramid plot
pyramid.plot(top25_df$x, top25_df$y, 
             labels = top25_df$labels, 
             main = "Words in Common",
             gap = 8, laxly = NULL, 
             raxlab = NULL, unit = NULL,
             top.labels = c("Chardonnay", 
                            "Words", 
                            "Coffee")
)

pyramid_plot.png

Text Mining with Bag-of-Words in R

Word networks

# Create word network
word_associate(coffee_tweets$text,
    match.string = c("barista"), 
    stopwords = c(Top200Words, "coffee", "amp"), 
    network.plot = TRUE, 
    cloud.colors = c("gray85", "darkred"))

# Add title
title(main = "Barista Coffee Tweet Associations")

word_network.png

Text Mining with Bag-of-Words in R

Let's practice!

Text Mining with Bag-of-Words in R

Preparing Video For Download...