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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