Created
August 1, 2017 15:45
-
-
Save tjpalanca/23b4929b7e9be41f62e503cfe76212ad to your computer and use it in GitHub Desktop.
Create Articles vs Reactions Joyplot Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creating joyplot at: https://github.com/tjpalanca/facebook-news-analysis/raw/master/figs/09-reactions-vs-articles.gif | |
# Full rmarkdown file at: https://github.com/tjpalanca/facebook-news-analysis/blob/master/src/02-fb-topic-modeling.Rmd | |
# Prepare dataset for joy plot | |
posts_classification.dt %>% | |
inner_join( | |
posts.dt %>% | |
select( | |
document = post_id, | |
news_page = page_name, | |
attachment_title, | |
post_timestamp_utc | |
), | |
by = "document" | |
) %>% | |
inner_join(topics_mapping.dt, by = "topic") %>% | |
filter( | |
!topic_name %in% | |
c("Filipino News", "Filipino Showbiz", | |
"Filipino Sports", "Breaking News") | |
) %>% | |
inner_join( | |
reactions.dt %>% | |
group_by(document = object_id) %>% | |
summarise(reactions = sum(count)) %>% | |
ungroup(), | |
by = "document" | |
) %>% | |
group_by( | |
topic_name, | |
# 8 hour time difference (Manila) | |
week = floor_date(post_timestamp_utc + hours(8), "week") | |
) %>% | |
summarise( | |
num_articles = sum(allocation), | |
num_reactions = sum(reactions * allocation) | |
) %>% | |
mutate(peak = week[num_articles == max(num_articles)]) %>% | |
ungroup() %>% | |
arrange(peak, week) %>% | |
mutate( | |
topic_name = factor(topic_name, levels = rev(unique(topic_name))), | |
# ID needed for linking back information after tweening frames | |
id = as.character(row_number()), | |
# Maximum height for ridgeline is 9 | |
articles_height = num_articles/max(num_articles) * 9, | |
reactions_height = num_reactions/max(num_reactions) * 9 | |
) -> | |
posts_time_trend.dt | |
# Prepare animated dataset | |
bind_rows( | |
# Articles | |
posts_time_trend.dt %>% | |
transmute(id, time = 0, ht = articles_height , op = 0), | |
# Reactions | |
posts_time_trend.dt %>% | |
transmute(id, time = 10, ht = reactions_height, op = 1), | |
# Return to articles | |
posts_time_trend.dt %>% | |
transmute(id, time = 20, ht = articles_height , op = 0), | |
) %>% | |
# Add cubic easing | |
mutate(ease = "cubic-in-out") %>% | |
tween_elements("time", "id", "ease") %>% | |
# Add back the topic name and week | |
inner_join( | |
posts_time_trend.dt %>% select(topic_name, week, id), | |
by = c(".group" = "id") | |
) -> | |
posts_time_trend.adt | |
# Create labels above the chart (what news outlets post vs what we react to) | |
posts_time_trend.adt %>% | |
group_by(.frame) %>% | |
summarise( | |
opacity = mean(op), | |
range_week = max(week) - min(week), | |
min_week = min(week), | |
num_topics = n_distinct(topic_name) | |
) %>% | |
ungroup() -> | |
posts_time_trend_labels.adt | |
# Create animated pot | |
posts_time_trend.adt %>% | |
ggplot(aes(x = week, y = topic_name, height = ht, | |
fill = topic_name, frame = .frame)) + | |
geom_ridgeline(color = NA, alpha = 0.5) + | |
geom_text( | |
data = posts_time_trend_labels.adt, | |
aes(x = min_week + 0.25 * range_week, | |
y = num_topics + 3.5, | |
alpha = 1 - opacity, | |
frame = .frame), | |
label = "What news outlets publish", color = "forestgreen", | |
family = "Roboto", fontface = "bold", size = 9, | |
inherit.aes = FALSE | |
) + | |
geom_text( | |
data = posts_time_trend_labels.adt, | |
aes(x = min_week + 0.75 * range_week, | |
y = num_topics + 3.5, | |
alpha = opacity, | |
frame = .frame), | |
label = "What we react to", color = "firebrick", | |
family = "Roboto", fontface = "bold", size = 9, | |
inherit.aes = FALSE | |
) + | |
geom_text( | |
data = posts_time_trend_labels.adt, | |
aes(x = min_week + 0.55 * range_week, | |
y = num_topics + 3.5, | |
frame = .frame), | |
label = "VS", | |
family = "Roboto", fontface = "bold", size = 7, | |
inherit.aes = FALSE | |
) + | |
scale_x_datetime(expand = c(0, 0)) + | |
theme( | |
legend.position = "none", | |
panel.border = element_blank(), | |
axis.title = element_blank(), | |
panel.grid.major.x = element_blank(), | |
panel.grid.minor.x = element_blank(), | |
plot.caption = element_text(size = 9) | |
) + | |
labs( | |
caption = " | |
DATA SOURCE: Facebook Graph API | |
CHART NOTE: Topics were derived through Latent Dirichlet Allocation (LDA) with 40 topics for Facebook news articles in 2016 for the Philippines | |
CHART BY: TROY JAMES PALANCA | TJPALANCA.COM | |
" | |
) -> | |
posts_time_trend.agg | |
gganimate( | |
p = posts_time_trend.agg, | |
filename = "../figs/09-reactions-vs-articles.gif", | |
title_frame = FALSE, | |
interval = 1/10, | |
ani.width = 900, | |
ani.height = 600 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment