Skip to content

Commit

Permalink
Added new function top_n_mentions()
Browse files Browse the repository at this point in the history
  • Loading branch information
Quiet27 committed Jul 15, 2022
1 parent c441652 commit abbe9f7
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(num_tweets_by_timeperiod)
export(num_tweets_by_username)
export(num_users_by_timeperiod)
export(top_n_hashtags)
export(top_n_mentions)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
Expand Down
55 changes: 55 additions & 0 deletions R/top_n_mentions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' Plot top n account mentions
#'
#' @description Create a ggplot2 bar chart of the number of times the top `n`
#' accounts (ties for `n`th position will be included) were mentioned in
#' tweets.
#'
#' @param sqlite_con [Class SQLiteConnection](https://rsqlite.r-dbi.org/reference/sqliteconnection-class)
#' object that is a connection to an SQLite .db file created by the
#' [tidy-tweet package](https://github.com/QUT-Digital-Observatory/tidy_tweet).
#' The database contains a collection of tweets in relational tables. This can
#' be created with [tweetexploR::connect_to_sqlite_db()].
#'
#' @param n Number of accounts to be plotted. Note, ties will be included.
#'
#' @return ggplot2 plot.
#'
#' @importFrom dplyr mutate rename group_by summarise n slice_max
#'
#' @importFrom ggplot2 ggplot aes geom_col labs theme
#'
#' @importFrom stringr str_to_lower
#'
#' @importFrom rlang .data
#'
#' @importFrom stats reorder
#'
#' @examples
#' \dontrun{
#'
#' top_n_mentions(sqlite_con, n = 10)
#' my_plot <- top_n_mentions(sqlite_con, 20)
#'
#' }
#'
#' @export

top_n_mentions <- function(sqlite_con, n) {
DBI::dbGetQuery(sqlite_con,
"SELECT username, source_id
FROM mention
WHERE source_type = 'tweet';") %>%
mutate(tag = str_to_lower(.data$username)) %>%
rename(account = .data$username) %>%
group_by(.data$account) %>%
summarise(mentions = n()) %>%
slice_max(n = n, order_by = .data$mentions, with_ties = TRUE) %>%
ggplot(aes(x = reorder(.data$account, .data$mentions), .data$mentions)) +
geom_col() +
labs(title = paste0("Top ", n, " accounts mentioned in tweets"),
y = "Number of tweets") +
configure_y_axis() +
ggplot2::coord_flip() +
configure_ggplot_theme() +
theme(axis.title.y = ggplot2::element_blank())
}
34 changes: 34 additions & 0 deletions man/top_n_mentions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions tests/testthat/_snaps/top_n_mentions/top-n-mentions-10.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/testthat/test-top_n_mentions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Connect to sqlite .db file
sqlite_con <- connect_to_sqlite_db(test_path("fixtures", "auspol-test.db"))


test_that("result is a ggplot2 object", {
expect_true(ggplot2::is.ggplot(top_n_mentions(sqlite_con, 10)))
})


test_that("ggplot2 plot has expected output", {
vdiffr::expect_doppelganger("top_n_mentions_10",
top_n_mentions(sqlite_con, 10))
})


# Disconnect from database
DBI::dbDisconnect(sqlite_con)

0 comments on commit abbe9f7

Please sign in to comment.