Skip to content

Instantly share code, notes, and snippets.

@macieksk
Last active March 15, 2017 13:06
Show Gist options
  • Save macieksk/007abbe35f718c3a2aad to your computer and use it in GitHub Desktop.
Save macieksk/007abbe35f718c3a2aad to your computer and use it in GitHub Desktop.
Draw a word cloud in R
################################################################################
#Owoc
#words <- c('Workshops', 'Workshops', 'Standardized curriculum', 'Review of studies', 'Review of studies', 'Free access to journals', 'Workshops', 'Meeting others')
#v <- sort(table(words),decreasing=TRUE)
nwords <- c(
'financial support',
'free access to journals',
'meeting others',
'observerships',
'review of studies',
'standardized curriculum',
'webinar e-learning',
'workshops'
)
counts<-c(3,2,3,1,6,2,6,10)
d <- data.frame(word = nwords,freq=counts,stringsAsFactors=FALSE)
bqvec<-sapply(1:NROW(d),function(i)as.expression(
bquote(.(d[i,"word"])[.("(")*.(d[i,"freq"])*.(")")])
#bquote(.(d[i,"word"])[.(d[i,"freq"])])
))
################################################################################
#Draw cloud
library(wordcloud)
library(RColorBrewer)
pal <- brewer.pal(9, "BuGn")
pal <- pal[-(1:2)]
png('owoccloud.png',width=1600,height=1200)
wordcloud(bqvec,d$freq+3, scale=c(8,.8),min.freq=1,max.words=100, random.order=F, rot.per=.01, colors=pal, vfont=c("sans serif","plain"))
dev.off()
################################################################################
# #XKCD processing
# library(tm)
# library(RXKCD)
# path <- system.file("xkcd", package = "RXKCD")
# datafiles <- list.files(path)
# #deprecated
# #xkcd.df <- read.csv(file.path(path, datafiles))
# print(load(file.path(path, datafiles))) # prints created object: xkcd.df
# xkcd.df <- data.frame(xkcd.df[, 3])
# xkcd.corpus <- Corpus(DataframeSource(xkcd.df))
# xkcd.corpus <- tm_map(xkcd.corpus, removePunctuation)
# xkcd.corpus <- tm_map(xkcd.corpus, tolower)
# xkcd.corpus <- tm_map(xkcd.corpus, function(x) removeWords(x, stopwords("english")))
# tdm <- TermDocumentMatrix(xkcd.corpus)
# m <- as.matrix(tdm)
# v <- sort(rowSums(m),decreasing=TRUE)
# d <- data.frame(word = names(v),freq=v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment