0

I want to create a word cloud for following data.

Red     30
Brown   12
Black   16
Green   33
Yellow  18
Grey    19
White   11

My word cloud should look like this:

enter image description here

In which words are alphabetically sorted and the font of the words is according to the values corresponding to the second column.

zx8754
  • 52,746
  • 12
  • 114
  • 209
jaikamal
  • 61
  • 4
  • 1
    have a look at `order` and http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns#answer-1296745 – Rentrop Nov 25 '16 at 13:34
  • @Floo0 I don't want to order column . I want to create a word cloud which is alphabetically sorted. As far as I know wordcloud() in R creates a wordcloud in random manner. If random.order set to false, word cloud will be plotted in decreasing frequency(not alphabetically) – jaikamal Nov 25 '16 at 13:37

1 Answers1

2

We can separate each word into letters then assign size per each letter and plot using ggplot2::geom_text:

library(ggplot2) # ggplot2_2.2.0

# data
df1 <- read.table(text ="
Red     30
Brown   12
Black   16
Green   33
Yellow  18
Grey    19
White   11", stringsAsFactors = FALSE)

colnames(df1) <- c("col", "size")
# order based on value of size
df1 <- df1[order(df1$col), ]

# separate into letters add size
datPlot <- 
  do.call(rbind,
  lapply(seq(nrow(df1)), function(i){
    myLetter <- c(".", unlist(strsplit(df1$col[i], split = "")))
    data.frame(myLetter = myLetter,
               size = c(10, rep(df1$size[i], length(myLetter) - 1)))
    }))
# each letter gets a sequential number on x axis, y is fixed to 1
datPlot$x <- seq(nrow(datPlot))
datPlot$y <- 1

# plot text
ggplot(datPlot, aes(x, y, label = myLetter, size = size/3)) +
  geom_text(col = "#F89443") +
  scale_size_identity() +
  theme_void()

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209