-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |