[R] Placement of words in a word cloud as per its occurance.

Jim Lemon drjimlemon at gmail.com
Wed May 18 00:21:19 CEST 2016


Hi Shailaja,
If you just want a line of words, it's not too difficult if you have
the word frequencies:

# take a common sentence
sentence<-"The quick brown fox jumps over the lazy dog"
words<-unlist(strsplit(sentence," "))
# make up some word frequencies
wordfreq<-c(10,1,2,2,3,4,10,6,5)
library(plotrix)
# scale the frequencies into a convenient range
wordcex<-rescale(wordfreq,c(0.5,2))
x11(height=4)
# a simple function to display a vector of words in different sizes
wordline<-function(words,wordcex,space=0.005) {
 plotxy<-par("usr")
 x<-plotxy[1]
 y<-sum(plotxy[3:4])/2
 space<-diff(plotxy[1:2])*space
 for(i in 1:length(words)) {
  par(cex=wordcex[i])
  text(x,y,words[i],adj=0)
  x<-x+strwidth(words[i])+space
 }
}
# get an empty plot
plot(1:10,type="n",axes=FALSE,xlab="",ylab="")
# display the words
wordline(words,wordcex)

The function can be elaborated to fit the sentence into the available
space, break it into lines, all that sort of thing.

Jim


On Tue, May 17, 2016 at 9:09 PM, shaila  shailaja
<shaila_jm at rediffmail.com> wrote:
> Dear R help subscribers,
>
>
>
>
> I am working on a word cloud where in I want the words to appear in the same order as in the sentence/text.
>
> I only know the random.order - which plots words in random order. If false, they will be plotted in decreasing frequency.
>
>  
>
> Any help is most welcome.
>
>  
>
> Regards
>
> Shailaja
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list