在stm教程的第18页中
https://cran.r-project.org/web/packages/stm/vignettes/stmVignette.pdf
绘制了预期的主题比例
plot(poliblogPrevFit, type = "summary", xlim = c(0, .3))
其中 poliblogPrevFit
poliblogPrevFit <- stm(documents = out$documents, vocab = out$vocab,
+ K = 20, prevalence =~ rating + s(day),
+ max.em.its = 75, data = out$meta,
+ init.type = "Spectral")
我试图找出如何在绘图中显示百分比值,我正在尝试添加plot函数的text
,但是没有用..如何在右边添加值情节中每个条形的哪个?
答案 0 :(得分:1)
要使您的想法使用所需的概率,首先要使用由stm主题模型生成的特定矩阵: Theta 。它基本上向您显示文档属于主题的概率。其次,您需要主题标签(如果您要坚持主题1,主题2等),以将值关联到。 我将代码与我自己的数据放在一起,但是它也适用于您的数据。请记住,也许要更改一些内容以使代码适用于您自己的特定数据。
## Put labels in a vector
labels <- c("Buffy", "Vampire", "Slayer", "Mr. Pointy")
## Include here your own labels, you probably have more than four
## Extract theta from the stm-model
df <- data.frame(labels)
proportion <- as.data.frame(colSums(stm_topics$theta/nrow(stm_topics$theta)))
df <- cbind(df, proportion)
colnames(df) <- c("Labels", "Probability")
## Sort the dataframe
df <- df[order(-proportion), ]
df$Labels <- factor(df$Labels, levels = rev(df$Labels))
df$Probability <- as.numeric(df$Probability)
df$Probability <- round(df$Probability, 4)
## Plot graph
ggplot(df, aes(x = Labels, y = Probability)) +
geom_bar(stat = "identity") +
scale_y_continuous(breaks = c(0, 0.15), limits = c(0, 0.15), expand = c(0, 0)) + #change breaks and limits as you need
coord_flip() +
geom_text(aes(label = scales::percent(Probability)), #Scale in percent
hjust = -0.25, size = 4,
position = position_dodge(width = 1),
inherit.aes = TRUE) +
theme(panel.border = element_blank())