我想从Textmining with R web教科书创建一个绘图,但使用我的数据。它主要搜索每年的顶级词汇并对它们进行图表(图5.4:http://tidytextmining.com/dtm.html)。我的数据比他们开始使用的数据要干净一点,但我对R是新手。我的数据有一个2016-01-01格式的日期列(日期类)。我只是自2016年有数据,所以我希望做同样的事情,但更精细,(按月或按天IE)如何从列中提取月份
library(tidyr)
year_term_counts <- inaug_td %>%
extract(document, "year", "(\\d+)", convert = TRUE) %>%
complete(year, term, fill = list(count = 0)) %>%
group_by(year) %>%
mutate(year_total = sum(count))
year_term_counts %>%
filter(term %in% c("god", "america", "foreign", "union", "constitution",
"freedom")) %>%
ggplot(aes(year, count/year_total)) +
geom_point() +
geom_smooth() +
facet_wrap(~ term, scales = "free_y") +
scale_y_continuous(labels = scales::percent_format()) +
ylab("% frequency of word in inaugural address")
的想法是,我会选择我的具体的话从我的文字,看到他们如何在几个月内改变。
谢谢!
欢迎来到SO:你有没有尝试打破'year_term_counts'函数检查中间步骤?你是否按照你的期望建立了结果?这将有助于我们看到一些数据。 –
您应该考虑在'lubridate'包中使用'month'函数来创建一个包含月份的整个列。 – ccapizzano
我会查看月份功能,谢谢! – Alex