Skip to content

Instantly share code, notes, and snippets.

@ikashnitsky
Last active June 1, 2020 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ikashnitsky/fca775ca0197622e673b2c58b4a78935 to your computer and use it in GitHub Desktop.
Save ikashnitsky/fca775ca0197622e673b2c58b4a78935 to your computer and use it in GitHub Desktop.
#===============================================================================
# 2020-05-26 -- twitter
# who I like
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
library(tidyverse)
library(rtweet)
library(ggdark)
library(rcartocolor)
library(hrbrthemes); import_roboto_condensed()
library(showtext)
showtext_auto()
# data processing adapted from:
# https://gist.github.com/fernandobarbalho/189c1f8d62f1e6c396a7c5e22e8f9a2c
df_likes <- rtweet::get_favorites("ikashnitsky", n = 3000)
users_favorite<-
df_likes %>%
group_by(screen_name, user_id) %>%
summarise(
quant_fav= n()
)
users_favorite %>%
ungroup() %>%
top_n(15,quant_fav) %>%
mutate(screen_name = reorder(screen_name,quant_fav)) %>%
ggplot(aes(y = screen_name, x = quant_fav, fill = quant_fav)) +
geom_col() +
dark_theme_minimal(base_family = font_rc, base_size = 16) +
scale_fill_carto_c(palette = "Mint", direction = -1, guide = NULL)+
labs(
title = "Top 15 accounts I like more often",
y = "@ user names",
x= "# of tweets liked by @ikashnitsky"
)
ggsave("my-twitter-likes.png", width = 7, height = 5)
# analyse the number of likes in time -------------------------------------
library(lubridate)
df_days <-
df_likes %>%
transmute(
created_at,
date = created_at %>% as_date() %>% ymd,
year = created_at %>% year(),
month = created_at %>% month(label = T),
week = created_at %>% week(),
weekday = created_at %>% wday(label = T, abbr = T, week_start = 1)
)
df_days %>%
mutate(year_week = paste0(year, "-", week)) %>%
group_by(year, week, year_week) %>%
summarise(
quant_fav= n()
) %>%
ungroup() %>%
arrange(year, week) %>%
mutate(week = week %>% as_factor %>% fct_inorder()) %>%
ggplot(aes(x = week, y = quant_fav, fill = quant_fav)) +
annotate(geom = "rect", xmin = -Inf, xmax = 4.5, ymin = -Inf, ymax = Inf,
color = NA, fill = "yellow", alpha = .05)+
geom_col() +
dark_theme_minimal(base_family = font_rc, base_size = 16) +
scale_fill_carto_c(palette = "Mint", direction = -1, guide = NULL)+
labs(
title = "My recent liking activity on Twitter",
x = "week of the year",
y = "# of likes by @ikashnitsky"
)+
annotate(geom = "text", x = c("50", "1"), y = 250,
size = 6, hjust = 0, family = font_rc,
label = c("2019", "2020"))
ggsave("my-twitter-likes-by-week.png", width = 7, height = 5)
# by week day -------------------------------------------------------------
# calendar plot of likes tolls by day
df_days %>%
group_by(date, year, month, week, weekday) %>%
summarise(
quant_fav= n()
) %>%
ungroup() %>%
ggplot(aes(week, weekday, fill = quant_fav))+
geom_tile()+
# scale_fill_viridis_c("#likes")+
scale_fill_carto_c("#likes", palette = "PurpOr", direction = -1)+
scale_x_continuous(breaks = c(1:9, 20, 30, 52))+
facet_grid(~year+month, scales = "free")+
dark_theme_minimal(base_family = font_rc, base_size = 16) +
theme(panel.grid = element_blank())+
labs(x = "week of the year", y = "week day",
title = "My recent liking activity on Twitter")
ggsave("my-twitter-likes-calendar.png", width = 7, height = 5)
# by hour -----------------------------------------------------------------
df_likes %>%
transmute(
created_at,
hour = created_at %>% hour(),
year = created_at %>% year(),
month = created_at %>% month(label = T),
weekday = created_at %>% wday(label = T, abbr = T, week_start = 1)
) %>%
group_by(hour) %>%
summarise(
quant_fav= n()
) %>%
ungroup() %>%
mutate(prop = quant_fav / sum(quant_fav)) %>%
ggplot(aes(x = hour, y = prop, fill = prop)) +
geom_col(position = position_nudge(x = .5)) +
scale_x_continuous(breaks = c(6, 12, 18))+
scale_y_percent()+
scale_fill_carto_c(palette = "Mint", direction = -1, guide = NULL)+
dark_theme_minimal(base_family = font_rc, base_size = 16) +
theme(panel.grid.minor = element_blank())+
labs(
title = "My recent liking activity on Twitter",
x = "hour of the day",
y = "proportion of likes",
caption = "@ikashnitsky"
)
ggsave("my-twitter-likes-by-hour.png", width = 7, height = 5)
# also facet by week day
df_likes %>%
transmute(
created_at,
hour = created_at %>% hour(),
year = created_at %>% year(),
month = created_at %>% month(label = T),
weekday = created_at %>% wday(label = T, abbr = T, week_start = 1)
) %>%
group_by(weekday, hour) %>%
summarise(
quant_fav= n()
) %>%
ungroup() %>%
group_by(weekday) %>%
mutate(prop = quant_fav / sum(quant_fav)) %>%
ungroup() %>%
ggplot(aes(x = hour, y = prop, fill = prop)) +
geom_col(position = position_nudge(x = .5)) +
scale_x_continuous(breaks = c(6, 12, 18))+
scale_y_percent()+
scale_fill_carto_c(palette = "Mint", direction = -1, guide = NULL)+
facet_wrap(~weekday, ncol = 5)+
dark_theme_minimal(base_family = font_rc, base_size = 16) +
theme(panel.grid.minor = element_blank())+
labs(
title = "My recent liking activity on Twitter",
x = "hour of the day",
y = "proportion of likes",
caption = "@ikashnitsky"
)
ggsave("my-twitter-likes-by-hour-weekday.png", width = 7, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment