2016-11-25 69 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-dataframe-by-columns#answer-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