结构主题模型(stm包)使用绘图函数绘制百分比值

问题描述 投票:0回答:1

在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")

我试图找出如何在图中显示百分比值,我正在尝试添加绘图函数的text但是不工作..如何在图中每个条的右边添加值?

r plot topic-modeling
1个回答
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())
© www.soinside.com 2019 - 2024. All rights reserved.