R 创建按字母顺序排序的单词云

R 创建按字母顺序排序的单词云,r,word-cloud,R,Word Cloud,我想为以下数据创建一个word cloud Red 30 Brown 12 Black 16 Green 33 Yellow 18 Grey 19 White 11 我的word cloud应该如下所示: 其中单词按字母顺序排序,单词的字体根据第二列对应的值。我们可以将每个单词分成字母,然后为每个字母指定大小,并使用ggplot2::geom_text进行绘图: library(ggplot2) # ggplot2_2.2.0 # data df1 <

我想为以下数据创建一个word cloud

Red     30
Brown   12
Black   16
Green   33
Yellow  18
Grey    19
White   11
我的word cloud应该如下所示:


其中单词按字母顺序排序,单词的字体根据第二列对应的值。

我们可以将每个单词分成字母,然后为每个字母指定大小,并使用
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()
库(ggplot2)#ggplot2_2.2.0
#资料

df1我们可以将每个单词分成字母,然后为每个字母指定大小,并使用
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()
库(ggplot2)#ggplot2_2.2.0
#资料

df1查看一下
order
和@Floo0我不想订购列。我想创建一个按字母顺序排序的单词云。据我所知,R中的wordcloud()以随机方式创建wordcloud。如果random.order设置为false,单词cloud将以递减的频率(不是字母顺序)绘制。请查看
顺序
和@Floo0我不想排序列。我想创建一个按字母顺序排序的单词云。据我所知,R中的wordcloud()以随机方式创建wordcloud。如果random.order设置为false,单词cloud将以递减的频率绘制(不是按字母顺序)