【问题标题】:Structural topic model (stm package) plot percentage values using plot function结构主题模型(stm包)使用绘图函数绘制百分比值
【发布时间】:2019-08-03 01:30:39
【问题描述】:

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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      相关资源
      最近更新 更多