【发布时间】:2021-05-13 15:58:49
【问题描述】:
在 R 中进行 lda 主题建模后,一些单词具有相同的 beta 值。因此,在绘制结果时将它们一起列出。这会导致结果重叠,有时甚至无法读取。
有没有办法将每个主题显示的字数限制为特定数字? 在我的虚拟数据集中,一些单词具有相同的 beta 值。我想告诉 R,它应该每个主题只显示 3 个单词,或者根据需要显示任何指定的数字。
目前我用来绘制结果的代码如下所示:
top_terms %>% # take the top terms
group_by(topic) %>%
mutate(top_term = term[which.max(beta)]) %>%
mutate(term = reorder(term, beta)) %>%
head(3) %>% # I tried this but that only works for the first topic
ggplot(aes(term, beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ top_term, scales = "free") +
labs(x = NULL, y = "Beta") + # no x label, change y label
coord_flip() # turn bars sideways
我尝试使用 head(3) 解决问题,但仅适用于第一个主题。
我需要的是类似的东西,它不会忽略所有其他主题。
最好的问候。 保持安全,保持健康。
注意:top_terms 是一个小标题。
样本数据:
topic term beta
(int) (chr) (dbl)
1 book 0,9876
1 page 0,9765
1 chapter 0,9654
1 author 0,9654
2 sports 0,8765
2 soccer 0,8654
2 champions 0,8543
2 victory 0,8543
3 music 0,9543
3 song 0,8678
3 artist 0,7231
3 concert 0,7231
4 movie 0,9846
4 cinema 0,9647
4 cast 0,8878
4 story 0,8878
dput的样本数据
top_terms <- structure(list(topic = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L), term = c("book", "page", "chapter",
"author", "sports", "soccer", "champions", "victory", "music",
"song", "artist", "concert", "movie", "cinema", "cast", "story"
), beta = c(0.9876, 0.9765, 0.9654, 0.9654, 0.8765, 0.8654, 0.8543,
0.8543, 0.9543, 0.8678, 0.7231, 0.7231, 0.9846, 0.9647, 0.8878,
0.8878)), row.names = c(NA, -16L), class = "data.frame")
【问题讨论】:
-
你想要
slice_max吗?
标签: r ggplot2 lda tibble topic-modeling