FigDraw 26. SCI文章中绘图词云图 (wordcloud)

图片

点击关注,桓峰基因


桓峰基因公众号推出基于R语言绘图教程并配有视频在线教程,目前整理出来的教程目录如下:

FigDraw 1. SCI 文章的灵魂 之 简约优雅的图表配色

FigDraw 2. SCI 文章绘图必备 R 语言基础

FigDraw 3. SCI 文章绘图必备 R 数据转换

FigDraw 4. SCI 文章绘图之散点图 (Scatter)

FigDraw 5. SCI 文章绘图之柱状图 (Barplot)

FigDraw 6. SCI 文章绘图之箱线图 (Boxplot)

FigDraw 7. SCI 文章绘图之折线图 (Lineplot)

FigDraw 8. SCI 文章绘图之饼图 (Pieplot)

FigDraw 9. SCI 文章绘图之韦恩图 (Vennplot)

FigDraw 10. SCI 文章绘图之直方图 (HistogramPlot)

FigDraw 11. SCI 文章绘图之小提琴图 (ViolinPlot)

FigDraw 12. SCI 文章绘图之相关性矩阵图(Correlation Matrix)

FigDraw 13. SCI 文章绘图之桑葚图及文章复现(Sankey)

FigDraw 14. SCI 文章绘图之和弦图及文章复现(Chord Diagram)

FigDraw 15. SCI 文章绘图之多组学圈图(OmicCircos)

FigDraw 16. SCI 文章绘图之树形图(Dendrogram)

FigDraw 17. SCI 文章绘图之主成分绘图(pca3d)

FigDraw 18. SCI 文章绘图之矩形树状图 (treemap)

FigDraw 19. SCI 文章中绘图之坡度图(Slope Chart)

FigDraw 20. SCI文章中绘图之马赛克图 (mosaic)

FigDraw 21. SCI文章中绘图之三维散点图 (plot3D)

FigDraw 22. SCI文章中绘图之核密度及山峦图 (ggridges)

FigDraw 23. SCI文章中绘图二维散点图与统计图组合

FigDraw 24. SCI文章中绘图二维直方图及组合图

FigDraw 25. SCI文章中绘图二维密度图及组合图

前言

如今,许多行业都注重数据可视化。如果我们想让文本数据以更“惊艳”的方式展示在别人面前,我们可以选择应用关键词云图。也许很多友友对词云的概念并不熟悉,今天就给大家简单解释一下。

1 | 词云含义

词云,也被称为文本云或标签云。在关键词云图中,特定文本单词出现的频率越高、占的面积越大,这就表明单词越重要。词云图可以使用普通的几何图形,或其他不规则的图片材料形状作为边界。词云图不仅可以用于公司数据分析,还可以用于媒体营销或广告设计。

2 | 词云作用

词云图可以让无聊的文本数据释放活力,实现更引人注目和直观的视觉效果,并可以立即传输关键信息。当我们必须进行深入的数据分析时,词云图可以帮助我们更好地区分不同元素的重要性;当我们的原始记录基于文本出现时,它可以避免数据不能通过图表直接显示的尴尬情况。

软件包安装

if(!require(tm))
  install.packages("tm")
if(!require(wordcloud))
  install.packages("wordcloud")

数据读取

R中的wordcloud包提供了绘制词云图的函数:wordcloud()、comparison.cloud()和commonality. cloud()。其中,用wordcloud(words,freq)函数绘制词云图时,只需要提供文本(words)和对应的频率(frequency);comparison.cloud(term.matrix)和cpommonality.cloud(term.matrix)可以绘制对比词云图,term. matrix是一个行名,代表文本,每列数值代表文本对应的频数的矩阵。

例子实操

一般参数设置

从字符直接绘制
library(tm)
library(wordcloud)
wordcloud(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62))

图片

从字符串里面直接绘图:
##### \t\t\tfrom character \t\t#####
wordcloud("Many years ago the great British explorer George Mallory, who 
was to die on Mount Everest, was asked why did he want to climb 
it. He said, \"Because it is there.\"

Well, space is there, and we're going to climb it, and the 
moon and the planets are there, and new hopes for knowledge 
and peace are there. And, therefore, as we set sail we ask 
God's blessing on the most hazardous and dangerous and greatest 
adventure on which man has ever embarked.",
    , random.order = FALSE)

图片

从文集中直接绘图:
data(crude)
crude <- tm_map(crude, removePunctuation)
crude <- tm_map(crude, function(x) removeWords(x, stopwords()))

##### \t\t\tfrom corpus \t\t#####
wordcloud(crude)

图片

从频率的个数直接绘图:
##### \t\tfrom frequency counts \t#####
tdm <- TermDocumentMatrix(crude)
m <- as.matrix(tdm)
v <- sort(rowSums(m), decreasing = TRUE)
d <- data.frame(word = names(v), freq = v)
wordcloud(d$word, d$freq)

图片

设置阈值,最小的频数为2:
# A bigger cloud with a minimum frequency of 2
wordcloud(d$word, d$freq, c(8, 0.3), 2)

图片

从单词绘制

现在让我们先用常用单词来尝试一下

# Now lets try it with frequent words plotted first
wordcloud(d$word, d$freq, c(8, 0.5), 2, , FALSE, 0.1)

图片

设置颜色:“BuGn”
##### \t\t\twith colors \t\t#####
require(RColorBrewer)
pal <- brewer.pal(9, "BuGn")
pal <- pal[-(1:4)]
wordcloud(d$word, d$freq, c(8, 0.3), 2, , FALSE, , 0.15, pal)

图片

设置颜色:“Dark2”
pal <- brewer.pal(6, "Dark2")
pal <- pal[-(1)]
wordcloud(d$word, d$freq, c(8, 0.3), 2, , TRUE, , 0.15, pal)

图片

设置颜色:随机色
# random colors
wordcloud(d$word, d$freq, c(8, 0.3), 2, , TRUE, TRUE, 0.15, pal)

图片

设置字体:vfont=c(“gothic english”,“plain”)
##### \t\t\twith font \t\t\t#####
wordcloud(d$word, d$freq, c(8, 0.3), 2, , TRUE, , 0.15, pal, vfont = c("gothic english",
    "plain"))

图片

设置字体:vfont=c(“script”,“plain”)
wordcloud(d$word, d$freq, c(8, 0.3), 2, 100, TRUE, , 0.15, pal, vfont = c("script",
    "plain"))

图片

设置字体:vfont=c(“serif”,“plain”)
wordcloud(d$word, d$freq, c(8, 0.3), 2, 100, TRUE, , 0.15, pal, vfont = c("serif",
    "plain"))

图片

两篇文章数据的对比
data(SOTU)
corp <- SOTU
corp <- tm_map(corp, removePunctuation)
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, removeNumbers)
corp <- tm_map(corp, function(x) removeWords(x, stopwords()))

term.matrix <- TermDocumentMatrix(corp)
term.matrix <- as.matrix(term.matrix)
colnames(term.matrix) <- c("SOTU 2010", "SOTU 2011")
comparison.cloud(term.matrix, max.words = 40, random.order = FALSE)

图片

设置不同的颜色:title.colors=c(“red”,“blue”),title.bg.colors=c(“grey40”,“grey70”)

comparison.cloud(term.matrix, max.words = 40, random.order = FALSE, title.colors = c("red",
    "blue"), title.bg.colors = c("grey40", "grey70"))

图片

设置匹配的颜色:match.colors=TRUE

comparison.cloud(term.matrix, max.words = 40, random.order = FALSE, match.colors = TRUE)

图片

两篇文章的共有部分

commonality.cloud(term.matrix, max.words = 40, random.order = FALSE)

图片

书中例子

单篇文章Paper1数据的展示

Paper1 <- paste(scan("Paper1.txt", what = character(0), sep = ""), collapse = " ")  #读入TXT 文档1
Paper2 <- paste(scan("Paper2.txt", what = character(0), sep = ""), collapse = " ")  #读入TXT 文档2
tmpText <- data.frame(c(Paper1, Paper2), row.names = c("Text1", "Text2"))
df_title <- data.frame(doc_id = row.names(tmpText), text = tmpText$c.Paper1..Paper2.)
ds <- DataframeSource(df_title)
# 创建一个数据框格式的数据源,首列是文档id(doc_id),第二列是文档内容
corp <- VCorpus(ds)
# 加载文档集中的文本并生成语料库文件
corp <- tm_map(corp, removePunctuation)  #清除语料库内的标点符号
corp <- tm_map(corp, PlainTextDocument)  #转换为纯文本
corp <- tm_map(corp, removeNumbers)  #清除数字符号
corp <- tm_map(corp, function(x) {
    removeWords(x, stopwords())
})  #过滤停止词库
term.matrix <- TermDocumentMatrix(corp)
# 利用TermDocumentMatrix()函数将处理后的语料库进行断字处理,生成词频权重矩阵

term.matrix <- as.matrix(term.matrix)  #频率
colnames(term.matrix) <- c("Paper1", "Paper2")
df <- data.frame(term.matrix)
write.csv(df, "term_matrix.csv")  #导出两篇文章的频率分析结果

df <- read.csv("term_matrix.csv", header = TRUE, row.names = 1)

wordcloud(row.names(df), df$Paper1, min.freq = 10, col = brewer.pal(8, "Dark2"),
    rot.per = 0.3)

图片

单篇文章Paper2数据的展示

wordcloud(row.names(df), df$Paper2, min.freq = 10, col = brewer.pal(8, "Dark2"),
    rot.per = 0.3)

图片

两篇文章数据的对比
comparison.cloud(df, max.words = 300, random.order = FALSE, colors = c("#00B2FF",
    "red"))

图片

两篇文章的共有部分
commonality.cloud(df, max.words = 100, random.order = FALSE, color = "#E7298A")

图片

软件包里面自带的例子,我这里都展示了一遍为了方便大家选择适合自己的图形,另外需要代码的将这期教程转发朋友圈,并配文“学生信,找桓峰基因,铸造成功的你!”即可获得!

桓峰基因,铸造成功的您!

有想进生信交流群的老师可以扫最后一个二维码加微信,备注“单位+姓名+目的”,有些想发广告的就免打扰吧,还得费力气把你踢出去!

References:
  1. wordcloud: https://rdocumentation.org/packages/wordcloud/versions/2.6

  2. 张杰. R 语言数据可视化之美 专业图表绘制指南

图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值