![見出し画像](https://assets.st-note.com/production/uploads/images/69782906/rectangle_large_type_2_1b63b61b4e239cf94e7671f095ea43ae.png?width=1200)
【R】rtweetでアカウントのツイートする曜日・日時のバブルプロットを作成する。
Twitterグラフィカル分析ツールwhotwiの
![](https://assets.st-note.com/img/1641996658647-bEkx6LjENB.png?width=1200)
これをRで作りたい。
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
rtweet,
lubridate
)
twitter_token <- create_token(
app = "YOUR_APP_NAME", # your app name
consumer_key = "****", # Consumer Key
consumer_secret = "****", # Consumer Secret
access_token = "****",
access_secret = "****"
)
account_activity <- function(account_name,
depth = 3200,
time_zone = "Japan") {
require(rtweet, quietly = TRUE)
require(tidyverse, quietly = TRUE)
require(lubridate, quietly = TRUE)
rtweet::get_timeline(
account_name,
n = depth,
retryonratelimit = FALSE
)[, 1:6] %>%
mutate(created_at2 = with_tz(created_at, tzone = time_zone)) %>%
mutate(tweet_hour = lubridate::hour(created_at2)) %>%
mutate(tweet_wday = lubridate::wday(created_at2, label = TRUE)) %>%
group_by(screen_name) %>%
group_by(source, .add = TRUE) %>%
group_by(tweet_hour, .add = TRUE) %>%
group_by(tweet_wday, .add = TRUE) %>%
mutate(hourly_tweet_count = n()) %>%
ungroup() %>%
group_by(source, tweet_hour, tweet_wday) %>%
slice(1) %>%
ungroup() -> g_dat
g_dat %>%
ggplot(
aes(
x = tweet_hour + 1,
y = tweet_wday,
size = hourly_tweet_count,
colour = source
)
) +
geom_point(size = g_dat$hourly_tweet_count / max(g_dat$hourly_tweet_count) * 25, alpha = 0.4) +
scale_size_continuous() +
scale_x_discrete(
limits = factor(c(0:23)),
breaks = c(0:23),
labels = c(0:23)
) +
expand_limits(x = c(0:23)) +
theme_classic() +
labs(
title = paste(
"Account activity: ",
account_name,
" (as at ",
as_datetime(Sys.Date()),
")",
sep = ""
),
x = "Hour of day",
y = "Day of week"
)
}
account_activity("@tokyo_bousai", 3200)
![](https://assets.st-note.com/img/1641996920961-eXJCyL9Gbz.png?width=1200)
それっぽいのができた。
参考にさせていただきました。