【发布时间】:2020-01-16 02:41:41
【问题描述】:
我有一个带有 word 列的数据框,我想用 ggplot 在条形图中显示文本中的前 10 个单词。
这是代码:
text_df %>% count(word, sort = TRUE) %>% top_n(10)
结果符合预期。现在我想在图表中显示:
text_df %>% count(word, sort = TRUE) %>% top_n(10) >%>
ggplot(aes(word, n)) + geom_col()
排序现在丢失了,十个单词以(对我来说)随机顺序出现。为什么排序会丢失?是不是我用错了命令?
【问题讨论】:
-
试试
text_df %>% count(word, sort = TRUE) %>% top_n(10) %>% mutate(word = forcats::fct_inorder(word))。这将通过使word和有序因子“锁定”排序,按照它出现在排序输出中的顺序。
标签: r