[关闭]
@agpwhy 2022-07-05T11:37:55.000000Z 字数 1197 阅读 291

王胖的生信笔记第四十七期:用211校训做个词云

马上上海的同学就要高考了,在这里祝大家发挥出应有水平,考到符合自己预期的专业。对于法八这个神奇的专业感兴趣的可以交流。

扯远了,在这个时间点上,写个应景的小笔记。用211大学(虽然这个有点过时了)的校训集合做个词云。

准备

  1. library(jiebaR)
  2. library(dplyr)
  3. library(readr)
  4. library(magrittr)
  5. library(wordcloud2)
  6. text_comments <- readLines(con = "./211大学的校训合辑.txt", encoding = "UTF-8")

jiebaR就是一个蛮好用的分词的R包。

开始处理词

具体的处理教程可以看下jiebaR的官方教程

  1. wk <- worker() #这个就是流程需要
  2. split_1 <- segment(text_comments, wk)
  3. split_1 <- split_1[!grepl("大学", split_1)] # 把大学名字删去
  4. split_combined <- sapply(split_1, function(x) {paste(x, collapse = " ")}) #拆分
  5. split_combined <- split_combined[nchar(split_combined)>1]
  6. split_combined <- split_combined[nchar(split_combined)<6] #去除过长过短的词
  7. comments_freq <- freq(split_combined)
  8. comments_freq <- comments_freq[order(comments_freq[,2],decreasing = TRUE),] #排序

然后就能出图了

wordcloud2(comments_freq, color = "random-light", size = 2, shape = "circle")

但是会发现储存有点问题。可以用之前提到过的webshot的方法。

  1. library("htmlwidgets")
  2. library("webshot")
  3. webshot::install_phantomjs()
  4. mygraph <- wordcloud2(comments_freq,
  5. color = "random-light", size = 2, shape = "circle")
  6. saveWidget(mygraph,"tmp.html",selfcontained = F)
  7. webshot("tmp.html","fig_1.pdf", delay =5, vwidth = 1600, vheight=1600)
  8. webshot("tmp.html","fig_1.png", delay =5, vwidth = 1600, vheight=1600)

最后结果如下啦:

fig_1

我到现在其实对复旦的校训校歌印象还是比交大更深,因为那时候高中几年每周都要唱的hhh。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注