R语言云雨图的绘制及代码
R语言云雨图是一种可视化数据的方式,可以展示云和雨的变化趋势。下面是一个使用R语言绘制云雨图的示例代码:
# 安装和加载所需的包
install.packages("wordcloud")
install.packages("tm")
install.packages("SnowballC")
library(wordcloud)
library(tm)
library(SnowballC)
# 导入文本数据
text <- c("云", "雨", "云朵", "降水", "气象", "天气")
# 创建词频矩阵
corpus <- Corpus(VectorSource(text))
corpus <- tm_map(corpus, tolower)
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removeWords, stopwords("english"))
corpus <- tm_map(corpus, stemDocument)
tdm <- TermDocumentMatrix(corpus)
m <- as.matrix(tdm)
v <- sort(rowSums(m), decreasing = TRUE)
d <- data.frame(word = names(v), freq = v)
# 绘制云雨图
wordcloud(d$word, d$freq, random.order = FALSE, colors = brewer.pal(8, "Dark2"))
用户评论