library(newsflash) # devtools::install_github("hrbrmstr/newsflash")
library(hrbrthemes)
library(tidyverse)
if (!file.exists("moartrends.rds")) {
from <- as.POSIXct("2017-09-07 00:00:00")
to <- as.POSIXct("2017-09-10 12:00:00")
trends <- top_trending_range(from, to)
write_rds(trends, "moartrends.rds")
} else{
trends <- read_rds("moartrends.rds")
}
select(trends, ts, station_top_topics) %>%
unnest() %>%
unnest() %>%
mutate(day = as.Date(ts)) %>%
rename(station=Station, topic=Topics) %>%
count(day, station, topic) %>%
group_by(station, day) %>%
top_n(10) %>%
slice(1:10) %>%
arrange(station, day, desc(n)) %>%
mutate(rnk = 10:1) %>%
ungroup() -> trends_by_station
select(trends_by_station, day, topic) %>%
count(day, topic) %>%
filter(n==1) %>%
mutate(color = "#4575b4", face="bold") %>%
select(-n) -> unique_topics_per_day
left_join(trends_by_station, unique_topics_per_day) %>%
mutate(
color = ifelse(is.na(color), "#2b2b2b", color),
face = ifelse(is.na(face), "plain", face)
) -> trends_by_station
ggplot(trends_by_station, aes(station, rnk, label=topic, size=n, group=day)) +
geom_text(aes(color=color, fontface=face), vjust=0.5, hjust=0.5) +
scale_x_discrete(expand=c(0,0.5), position="top") +
scale_y_discrete(expand=c(0,1)) +
scale_size(name=NULL, range=c(2,5)) +
scale_color_identity() +
facet_wrap(~day, scales="free_x", ncol=1, strip.position="bottom") +
labs(
x=NULL, y=NULL,
title="Top 10 Trending Topics Per-Station, Per-Day",
subtitle="Topic placed by rank and sized by frequency; Blue highlights == unique topics (in top 10) that day.\nNOTE Sep 10 retrieval capped at noon (hence smaller # memtions)",
caption="GDELT Television Explorer & #rstats newsflash package github.com/hrbrmstr/newsflash"
) +
theme_ipsum_rc(grid="") +
theme(axis.text.x=element_text(face="bold")) +
theme(axis.text.y=element_blank()) +
theme(panel.spacing.y = unit(3, "lines")) +
theme(legend.position=c(0.85, 1.05)) +
theme(legend.direction="horizontal") -> gg
gt <- ggplot_gtable(ggplot_build(gg))
gt$layout$clip[grepl("panel", gt$layout$name)] <- "off"
grid::grid.draw(gt)
