diff --git a/NAMESPACE b/NAMESPACE index 4d1c87e..14e180f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/top_n_retweets.R b/R/top_n_retweets.R index f33f0c2..6e80013 100644 --- a/R/top_n_retweets.R +++ b/R/top_n_retweets.R @@ -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 #' @@ -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()) diff --git a/tests/testthat/_snaps/top_n_retweets/top-n-retweets-10.svg b/tests/testthat/_snaps/top_n_retweets/top-n-retweets-10-metrics-false.svg similarity index 100% rename from tests/testthat/_snaps/top_n_retweets/top-n-retweets-10.svg rename to tests/testthat/_snaps/top_n_retweets/top-n-retweets-10-metrics-false.svg diff --git a/tests/testthat/_snaps/top_n_retweets/top-n-retweets-10-metrics-true.svg b/tests/testthat/_snaps/top_n_retweets/top-n-retweets-10-metrics-true.svg new file mode 100644 index 0000000..fdc7e05 --- /dev/null +++ b/tests/testthat/_snaps/top_n_retweets/top-n-retweets-10-metrics-true.svg @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Dan "Chris Bowen in one meeting has done more to +secure our energy future than t +RT @SquizzSTK: Dan "Chris Bowen in one meeting +has done more to secure our energ +Department of Social Services secretary Kathryn +Campbell: "I do not know what ro +BREAKING: Australia sends its first 4 of 14 +armoured personnel carriers as part +Imagine creating six lucrative jobs in your last +days of office, saying you’re r +RT @EddyJokovich: Imagine creating six lucrative +jobs in your last days of offic +The new National Minimum Wage will be $812.60 per +week. Okay. Good. + +Anyone wann +It’s pretty appalling the RBA had 63 meetings +with businesses about wages growth +On this day, it is worth remembering that +corporate media outlets played a key r +He’s right 👍🏾 https://t.co/FT23XWMcPy + + + + + + + + + + + + + + +0 +1,000 +2,000 +3,000 +Number of retweets +Top 10 retweeted tweets (Twitter metrics) + + diff --git a/tests/testthat/test-top_n_retweets.R b/tests/testthat/test-top_n_retweets.R index f655348..9acc90f 100644 --- a/tests/testthat/test-top_n_retweets.R +++ b/tests/testthat/test-top_n_retweets.R @@ -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)) })