使用R语言编写云雨图的代码示例

pleasure_79578 29 0 r 2023-09-01 15:09:06

下面是使用R语言编写云雨图的示例代码,供您参考:

library(wordcloud)
library(RColorBrewer)

# 读取文本数据
text <- readLines("data.txt", encoding = "UTF-8")

# 文本预处理
text <- gsub("[[:punct:]]", "", text)
text <- gsub("\\d+", "", text)
text <- gsub("\\s+", " ", text)

# 将文本转换为词频矩阵
text_matrix <- table(unlist(strsplit(text, " ")))

# 创建词云图
wordcloud(words = names(text_matrix), freq = text_matrix, random.order = FALSE, colors = brewer.pal(8, "Dark2"))

# 创建云雨图(可选)
# wordcloud.words <- names(text_matrix)[which(text_matrix > 10)]
# wordcloud(words = wordcloud.words, freq = text_matrix[wordcloud.words], random.order = FALSE, colors = brewer.pal(8, "Dark2"))

用户评论
请输入评论内容
评分:
暂无评论