2016-11-25 15 views
0

次のデータのための単語群を作成したい。アルファベット順にソートされた単語群を作成する

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

マイワードクラウドは次のようになります。

enter image description here

れる単語がアルファベット順にソートされ、単語のフォントが二列に対応する値に応じています。

+1

は 'order'とhttp://stackoverflow.com/questionsを見て/ 1296646/how-to-sort-a-data-by-columns#答え-1296745 – Rentrop

+0

@ Floo0私は列を注文したくありません。私はアルファベット順にソートされた単語雲を作成したい。私が知っている限り、Rのwordcloud()はランダムな方法でwordcloudを作成します。 random.order がfalseに設定されている場合、ワードクラウドは減少する頻度でプロットされます(アルファベット順ではありません) – jaikamal

答えて

2

私たちは、その後、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

+1

私によく見えます。 – BrodieG

関連する問題