Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Likes per tweet plot
# Analyze user's data downloaded from Twitter
library(jsonlite)
library(tidyverse)
# download your data from Twitter (Settings and Privacy, Your Twitter data), unzip the folder
setwd("~/Downloads/twitter-2018-10-29-6c18f5c0ab3605451f73cd0fd9ed186a2fd5b2caf1812ab747d42351da805656/")
json <- readLines("tweet.js")
json[[1]] <- "[ {" # remove illegal window.YTD.tweet.part0 =
tweets <- fromJSON(json)
names(tweets)
# parsing time
parse_date <- function(x) {
mdy_hms <- paste(
substr(x, 5,10),
substr(x, 27,30),
substr(x, 12,19),
sep = " ")
as.character(lubridate::mdy_hms(mdy_hms))
}
tweets$Time <- as.POSIXct(sapply(tweets$created_at, parse_date))
tweets$Likes <- as.numeric(tweets$favorite_count)
tweets$Retweets <- as.numeric(tweets$retweet_count)
tweets %>%
select(Time, Likes, Retweets, in_reply_to_status_id) %>%
filter(is.na(in_reply_to_status_id)) %>%
ggplot(aes(x = Time, y = Likes)) +
geom_point(alpha=0.9, color = "grey") +
geom_smooth(color = "red", fill = "pink", size = 2) +
theme_minimal() +
ylim(-1,30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment