ggplot用R对LDA中的β值进行排序。

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

我在LDA分析过程中画了一个图,但我想根据β值按降序得到图。

ap_topics <- tidy(ap_lda, matrix = "beta")
ap_lda <- LDA(dtm.new, k = 15, control = list(alpha=0.1,seed=1234))
ap_topics <- tidy(ap_lda, matrix = "beta")

ap_top_terms <- ap_topics %>%
  group_by(topic) %>%
  top_n(15, beta) %>%
  ungroup() %>%
  arrange(topic, -beta)

ap_top_terms %>%
  mutate(term = reorder(term, beta)) %>%
  ggplot(aes(term,beta, fill = factor(topic))) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ topic, scales = "free") +
  coord_flip()

我想让图中每个项的β值更容易看到。我应该怎么做?

ap_top_term结构

    > ap_top_terms
# A tibble: 150 x 3
   topic term       beta
   <int> <chr>     <dbl>
 1     1 hdr     0.0211 
 2     1 better  0.0101 
 3     1 content 0.00979
 4     1 dontt   0.00930
 5     1 thing   0.00749
 6     1 oled    0.00720
 7     1 black   0.00625
 8     1 model   0.00611
 9     1 make    0.00566
10     1 update  0.00556
# ... with 140 more rows

这是我现在的情节

请在此输入图片描述

r plot ggplot2 lda
1个回答
0
投票

也许这个博客能帮到你?https:/nitinahuja.github.io2017nps-exploratory-text-analysis。

top_terms %>% 
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill=factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free" ) +
coord_flip() +
guides(fill=FALSE) +
labs(title = "Terms in topics - By Comment", x = "Term", y = 
"Probability")

或者这个 https:/www.tidytextmining.comtopicmodeling.html 库(ggplot2)

top_terms %>%
mutate(term = reorder_within(term, beta, topic)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
scale_x_reordered()
© www.soinside.com 2019 - 2024. All rights reserved.