Skip to content

Instantly share code, notes, and snippets.

@ikashnitsky
Last active May 28, 2020 01:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikashnitsky/5e71785abe5aca42a793d068e4c8876f to your computer and use it in GitHub Desktop.
Save ikashnitsky/5e71785abe5aca42a793d068e4c8876f to your computer and use it in GitHub Desktop.
#===============================================================================
# 2020-05-27 -- twitter
# explore the timing of my twitter activity
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
library(tidyverse)
library(rtweet)
library(lubridate)
library(ggdark)
library(ggtext)
library(rcartocolor)
library(rcartocolor)
# get data -- luckily I have 3108 tweets
df_tweets <- get_timeline(user = "ikashnitsky", n = 3200)
save(df_tweets, file = "200527-my-tweets//ik-tweets.rda")
load("200527-my-tweets//ik-tweets.rda")
df_time <-
df_tweets %>%
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),
hour = created_at %>% hour()
)
# calendar plot of my tweets created between 2 and 6 am
df_time %>%
mutate(night_like = hour %in% 2:6) %>%
group_by(date, year, month, week, weekday) %>%
summarise(n = night_like %>% sum) %>%
ungroup() %>%
ggplot(aes(week, weekday, fill = !n==0))+
geom_tile()+
scale_fill_manual(values = c("grey", "cyan"), guide = NULL)+
scale_x_continuous(breaks = c(1:9, 20, 30, 52))+
facet_grid(year~month, scales = "free")+
dark_theme_minimal(base_family = "mono") +
theme(
text = element_text(face = 2),
panel.grid = element_blank(),
panel.spacing = unit(.5, "lines"),
plot.title = element_markdown(size = 20)
)+
labs(
x = "week of the year", y = "week day",
title = "On which days I <span style='color:#00FFFF'>tweeted between 2 and 6 am</span>",
caption = "@ikashnitsky"
)
ggsave("my-tweets-night.png", width = 8, height = 5)
# number of tweets by day
df_time %>%
group_by(date, year, month, week, weekday) %>%
summarise(n = n()) %>%
ungroup() %>%
ggplot(aes(week, weekday, fill = n))+
geom_tile()+
scale_fill_carto_c(palette = "Sunset", direction = -1,
guide = guide_colorbar(barwidth = 30, barheight = .5))+
scale_x_continuous(breaks = c(1:9, 20, 30, 52))+
facet_grid(year~month, scales = "free")+
dark_theme_minimal(base_family = "mono") +
theme(
text = element_text(face = 2),
panel.grid = element_blank(),
panel.spacing = unit(.5, "lines"),
plot.title = element_markdown(size = 22),
legend.position = "top"
)+
labs(
x = "week of the year",
y = "week day",
title = "My days on Twitter: <span style='color:#F3E79BFF'>more</span> and <span style='color:#5C53A5FF'>less</span> active",
caption = "@ikashnitsky",
fill = NULL
)
ggsave("my-tweets-n.png", width = 8, height = 5)
# just timing
df_time %>%
group_by(hour) %>%
summarise(n = n()) %>%
ungroup() %>%
mutate(prop = n / sum(n)) %>%
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 = "mono") +
theme(
text = element_text(face = 2),
panel.grid.minor = element_blank(),
plot.title = element_markdown(size = 22)
)+
labs(
title = "My tweets by hour",
x = "hour of the day",
y = "proportion of tweets",
caption = "@ikashnitsky"
)
ggsave("my-tweets-by-hour.png", width = 8, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment