我想绘制一个特定主题的比例如何随时间变化,但我在隔离单个主题和绘制时间方面遇到了一些麻烦,尤其是分别绘制多组文档时(让我们创建两组进行比较 - 期刊A和B)。我在一个名为dateConverter
的函数中保存了与这些日记相关联的日期。
这是我到目前为止所做的事情(非常感谢@scoa):
library(tm); library(topicmodels);
txtfolder <- "~/path/to/documents/"
source <- DirSource(txtfolder)
myCorpus <- Corpus(source, readerControl=list(reader=readPlain))
for (i in 1:10){
meta(myCorpus[[i]], tag = "origin") <- "A"
}
for (i in 11:length(myCorpus)){
meta(myCorpus[[i]], tag = "origin") <- "B"
}
dates <- do.call("c", dateConverter)
for (i in 1:length(myCorpus)){
meta(myCorpus[[i]], tag = "datetimestamp") <- dates[i]
}
dtm <- DocumentTermMatrix(myCorpus, control = list(minWordLength=3))
n.topics <- 10
lda.model <- LDA(dtm, n.topics)
terms(lda.model,10)
df <- data.frame(id=names(topics(lda.model)),
topic=posterior(lda.model),
date=as.POSIXct(unlist(lapply(meta(myCorpus,type="local",tag="datetimestamp"),as.character))),
origin=unlist(meta(myCorpus,type="local",tag="origin")) )
我如何绘制这些?
答案 0 :(得分:5)
这只是我之前回答的改编:
## Load the data
library(tm)
## Use built-in data set
data(acq)
myCorpus <- acq
## prepare the data
for (i in 1:25){
meta(myCorpus[[i]], tag = "origin") <- "A"
}
for (i in 26:length(myCorpus)){
meta(myCorpus[[i]], tag = "origin") <- "B"
}
dates <- sample(seq.Date(as.Date("2013-01-01"),as.Date("2014-01-01"),length.out=8),50, replace=TRUE)
for (i in 1:length(myCorpus)){
meta(myCorpus[[i]], tag = "datetimestamp") <- dates[i]
}
dtm <- DocumentTermMatrix(myCorpus, control = list(minWordLength=3))
library(topicmodels)
n.topics <- 5
lda.model <- LDA(dtm, n.topics)
terms(lda.model,10)
重塑数据以进行绘图。我对每组主题,日期和起源采取平均后验。
df <- data.frame(id=names(topics(lda.model)),
date=as.POSIXct(unlist(lapply(meta(myCorpus,type="local",tag="datetimestamp"),as.character))),
origin=unlist(meta(myCorpus,type="local",tag="origin")) )
dft <- cbind(df,posterior(lda.model)$topics)
library(dplyr)
library(tidyr)
M <- gather(dft,topic,value,-id,-date,-origin) %>%
group_by(topic,date,origin) %>%
summarize(value=mean(value))
剧情
library(ggplot2)
ggplot(M,aes(x=date,color=origin,y=value)) +
geom_point() +
geom_line() +
facet_grid(topic~origin)