使用LDA尝试进行主题建模。为了可视化每个文档中的主题,我尝试使用qplot,但收到错误和警告。
错误:stat_bin()不得与y美学一起使用。
警告消息:stat
已弃用
程序使用cora数据集。
This is the output plot I am looking for.
下面是代码段:
require("ggplot2")
require("reshape2")
require("lda")
# load documents and vocabulary
data(cora.documents)
data(cora.vocab)
theme_set(theme_bw())
# Number of topic clusters to display
K <- 10
# Number of documents to display
N <- 9
result <- lda.collapsed.gibbs.sampler(cora.documents,
K, ## Num clusters
cora.vocab,
25, ## Num iterations
0.1,
0.1,
compute.log.likelihood=TRUE)
# Get the top words in the cluster
top.words <- top.topic.words(result$topics, 5, by.score=TRUE)
# build topic proportions
topic.props <- t(result$document_sums) / colSums(result$document_sums)
document.samples <- sample(1:dim(topic.props)[1], N)
topic.props <- topic.props[document.samples,]
topic.props[is.na(topic.props)] <- 1 / K
colnames(topic.props) <- apply(top.words, 2, paste, collapse=" ")
topic.props.df <- melt(cbind(data.frame(topic.props),
document=factor(1:N)),
variable.name="topic",
id.vars = "document")
qplot(topic, value*100, fill=topic, stat = "identity",
ylab="proportion (%)", data=topic.props.df,
geom="histogram") +
theme(axis.text.x = element_text(angle=0, hjust=1, size=12)) +
coord_flip() +
facet_wrap(~ document, ncol=3)