Heart-shaped wordcloud, celebrating Colombia peace treaty

[This article was first published on R – AmitKohli.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)