Skip to content

Commit

Permalink
Added plot based on Twitter metrics for top_n_retweets()
Browse files Browse the repository at this point in the history
  • Loading branch information
Quiet27 committed Jul 15, 2022
1 parent 6f5b207 commit 4ed0f97
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 42 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(num_users_by_timeperiod)
export(top_n_hashtags)
export(top_n_mentions)
export(top_n_retweets)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
Expand Down
56 changes: 16 additions & 40 deletions R/top_n_retweets.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'
#' @return ggplot2 plot.
#'
#' @importFrom dplyr slice_max
#' @importFrom dplyr slice_max distinct
#'
#' @importFrom ggplot2 aes geom_col labs scale_x_discrete theme
#'
Expand Down Expand Up @@ -62,45 +62,21 @@ top_n_retweets <- function(sqlite_con, n, metrics = FALSE, tweet_chars = 80,
}

else if (metrics == TRUE) {
# Plot based on Twitter metrics
DBI::dbGetQuery(sqlite_con,
"SELECT retweet_count, text
FROM tweet;") %>%
distinct() %>%
slice_max(n = n, order_by = .data$retweet_count, with_ties = TRUE) %>%
ggplot(aes(x = reorder(substr(.data$text, 1, tweet_chars),
.data$retweet_count), .data$retweet_count)) +
geom_col() +
labs(title = paste0("Top ", n, " retweeted tweets (Twitter metrics)"),
y = "Number of retweets") +
scale_x_discrete(labels = scales::label_wrap(chars_per_line)) +
configure_y_axis() +
ggplot2::coord_flip() +
configure_ggplot_theme() +
theme(axis.title.y = ggplot2::element_blank())
}

}

## Top n retweeted tweets ####

### Based on number of retweets inside the tweets that were collected ####

# dbGetQuery(con,
# "SELECT retweeted_tweet_id, count(*) as `retweets`, text
# FROM tweet
# WHERE retweeted_tweet_id IS NOT NULL
# GROUP BY retweeted_tweet_id;") %>%
# slice_max(n = n, order_by = retweets, with_ties = TRUE) %>%
# ggplot(aes(reorder(substr(text, 1, tweet_chars), retweets), retweets)) +
# geom_col() +
# labs(title = paste0("Top ", n, " retweeted tweets (within collection)"),
# y = "Number of retweets") +
# scale_x_discrete(labels = label_wrap(chars_per_line))+
# my_y_axis +
# coord_flip() +
# my_theme +
# theme(axis.title.y = element_blank())


### Based on tweet.retweet_count (Twitter metrics) ####

# dbGetQuery(con,
# "SELECT retweet_count, text
# FROM tweet;") %>%
# distinct() %>%
# slice_max(n = n, order_by = retweet_count, with_ties = TRUE) %>%
# ggplot(aes(reorder(substr(text, 1, tweet_chars), retweet_count), retweet_count)) +
# geom_col() +
# labs(title = paste0("Top ", n, " retweeted tweets (Twitter metrics)"),
# y = "Number of retweets") +
# scale_x_discrete(labels = label_wrap(chars_per_line)) +
# my_y_axis +
# coord_flip() +
# my_theme +
# theme(axis.title.y = element_blank())
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions tests/testthat/test-top_n_retweets.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ test_that("result is a ggplot2 object", {


test_that("ggplot2 plot has expected output", {
vdiffr::expect_doppelganger("top_n_retweets_10",
top_n_retweets(sqlite_con, 10))
vdiffr::expect_doppelganger("top_n_retweets_10_metrics_false",
top_n_retweets(sqlite_con, 10, metrics = FALSE))
})


test_that("ggplot2 plot has expected output", {
vdiffr::expect_doppelganger("top_n_retweets_10_metrics_true",
top_n_retweets(sqlite_con, 10, metrics = TRUE))
})


Expand Down

0 comments on commit 4ed0f97

Please sign in to comment.