我正在寻找一种方法,以比较文字直方图的文件,属于一个文件夹语料库与几个文件网。我确实尝试过:
freq <- sort(colSums(as.matrix(dtm), group=Docs), decreasing=TRUE) 同时,也尝试了ggplot选项:
p <- p + geom_bar(stat="identity") + facet_wrap(~ Docs) 但令人遗憾的是,我错了。
下面是一个修改过的代码示例,但令人遗憾的是,我的3个文档的情节类似于一个文档,也没有被Docs分割:
c= c("hola como hola como hola como", "hola me fui hola me fui hola me fui hola me fui", "hola como estas hola como estas hola como estas" )
corpus= VCorpus(VectorSource(c))
dtm <- DocumentTermMatrix(corpus)
m <- as.matrix(dtm)
m
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
wf <- data.frame(word=names(freq), freq=freq)
p <- ggplot(subset(wf, freq>1), aes(word, freq))
p <- p + geom_bar(stat="identity")
p <- p + theme(axis.text.x=element_text(angle=45, hjust=1))
p 发布于 2018-07-01 08:23:59
创建wf的方式意味着您正在丢失您的文档,并且它们对ggplot2不可用。我将创建wf作为文档名称和dtm的组合。(如果你有一个很大的语料库,这里要小心。)然后,我把wf转换成一个长格式,所以ggplot对于ggplot2来说是非常好的格式。然后,当你创建你的地块时,你只需用你所需要的任何方式进行整理。在下面的示例中,我在文档之间拆分了图。
library(tm)
c= c("hola como hola como hola como", "hola me fui hola me fui hola me fui hola me fui", "hola como estas hola como estas hola como estas" )
corpus= VCorpus(VectorSource(c))
dtm <- DocumentTermMatrix(corpus)
wf <- data.frame(docs=Docs(dtm), as.matrix(dtm))
library(tidyr)
wf <- wf %>% gather(key = "terms", value = "freq", -docs)
library(ggplot2)
ggplot(wf, aes(terms, freq)) +
geom_bar(stat="identity") +
facet_wrap(~ docs) +
theme(axis.text.x=element_text(angle=45, hjust=1))

https://stackoverflow.com/questions/51121368
复制相似问题