我正尝试每年绘制最常用单词的条形图,但ggplot仅绘制一张图
word frequency Year
easy use easy use 9 2019
value money value money 8 2019
project management project management 4 2019
business management business management 3 2019
customer serviceâ€\u009d customer serviceâ€\u009d 3 2019
everything need everything need 3 2019
tail(unified)
word frequency Year
workflows support workflows support 1 2014
working people working people 1 2014
works helpful works helpful 1 2014
worth try1 worth try 1 2014
write invoices write invoices 1 2014
years research years research 1 2014
ggplot(head(unified,25), aes(reorder(word,-frequency), frequency)) +
geom_bar(stat = "identity") + facet_wrap(~Year) + theme(axis.text.x = element_text(angle=90, hjust=1)) + xlab("Bigrams") + ylab("Frequency") +
ggtitle("Most frequent bigrams for all years")
我的ggplot仅生成2019年的条形图。请帮助
答案 0 :(得分:0)
您可以使用dplyr::group_by
和dplyr::top_n
来选择年度排名前25位最常用的单词。
library(tidyverse)
n <- 1000
t <- 4
unified <- data.frame(
word = sample(letters[1:8], n * t, replace = TRUE),
Year = sort(rep(1:t, n))
) %>%
dplyr::group_by(Year) %>%
dplyr::count(word, name = "frequency") %>%
dplyr::arrange(Year, desc(frequency)) %>%
dplyr::top_n(25, frequency) %>%
dplyr::select(word, frequency, Year) %>%
dplyr::ungroup()
ggplot(unified, aes(reorder(word, -frequency), frequency)) +
geom_bar(stat = "identity") +
facet_wrap(~Year, scales="free") +
theme(axis.text.x = element_text(angle=90, hjust=1)) +
xlab("Bigrams") +
ylab("Frequency") +
ggtitle("Most frequent bigrams for all years")
由reprex package(v0.3.0)于2019-10-13创建