为了你的想法使用你需要的概率,首先,一个由 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())