Heart-shaped wordcloud, celebrating Colombia peace treaty

This is a lightening quick post just providing the script to draw a heart-shaped wordcloud, using the awesome wordcloud2 package. See the resulting image here:

colombia

 

Apparently, the original code allows you to fit a wordcloud to any shape, even custom shapes, but I didn’t find that functionality pushed out into R yet (a peace sign would have been awesome in this case).

In any event, here’s the code to accomplish this chart. Sorry for the very very short post, I’m working on something big! Stay tuned!

########################################################################
## Title: Draw heart-shaped wordcloud
## Date: 2016-09-26
########################################################################

## Load libraries
library(twitteR)
library(dplyr)
library(httr)
library(rvest)
library(wordcloud2) # devtools::install_github("lchiffon/wordcloud2")

## SETUP (you need to authenticate on twitter... put in your secret codes here)

setup_twitter_oauth("secret1", "secret2")


## THING TO LOOK FOR

topik<-"#PazEnColombia"

S1 = searchTwitteR(topik, n = 10000)

## Convert text df

S.df = do.call("rbind", lapply(S1, as.data.frame))

## Get only text and convert to lower

b <- unlist(strsplit(S.df$text," "))

b <- tolower(b)

## Get list of spanish stopwords
sw <- read_html("http://www.ranks.nl/stopwords/spanish")

sw <- html_nodes(sw,"td")

sw <- unlist(strsplit(html_text(sw)," "))

## add a few more stopwords and "RT", and eliminate ":"

sw <- c(sw,"de","la","rt","en","las","para","por","con","que","a","y")

sw <- gsub(":","",sw)

## 'niceify'

stopWords <- data.frame(word = sw)

table(b) %>% as.data.frame -> c

names(c) <- c("word","freq")

## Remove stopwords via anti_join, and infrequent matches

d <- anti_join(x = c,y=stopWords) %>%

arrange(desc(freq)) %>% filter(freq>2)

## Plot!

wordcloud2(d, size = 2,shape = 'cardioid')

Leave a Reply

%d bloggers like this: