Heart-shaped wordcloud, celebrating Colombia peace treaty

September 26, 2016
By

(This article was first published on R – AmitKohli.com, and kindly contributed to R-bloggers)

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')

To leave a comment for the author, please follow the link and comment on their blog: R – AmitKohli.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)