From d8aa8f3e9d0737686b385e2c254200aaa641dfce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?= <13123390+rempsyc@users.noreply.github.com> Date: Mon, 29 Apr 2024 20:32:25 +0200 Subject: [PATCH] add plotly_citation --- R/scatter_continent_year.R | 34 ++++++++++++++++++++++++++++++---- R/scatter_country_year.R | 13 ++++++++++--- R/waffle_continent.R | 7 ------- man/scatter_continent_year.Rd | 13 ++++++++++++- man/scatter_country_year.Rd | 13 ++++++++++++- 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/R/scatter_continent_year.R b/R/scatter_continent_year.R index b712ac0..6cd90b0 100644 --- a/R/scatter_continent_year.R +++ b/R/scatter_continent_year.R @@ -2,6 +2,8 @@ #' @param data The processed dataframe of data #' @param method Which method to use for the regression line, either "lm" (default) or "loess". #' @param plotly Logical, whether to use plotly for dynamic data visualization. +#' @param citation Optionally, a citation to add as a footer. +#' @param citation_size Font size of the citation. #' @param ... Further arguments passed to [rempsyc::nice_scatter] #' @examples #' \dontshow{ @@ -28,7 +30,7 @@ #' @importFrom rlang .data #' @export -scatter_continent_year <- function(data, method = "lm", plotly = TRUE, ...) { +scatter_continent_year <- function(data, method = "lm", plotly = TRUE, citation, citation_size = 15, ...) { insight::check_if_installed("RColorBrewer") data <- data %>% dplyr::mutate(missing = sum(is.na(.data$continent)) / dplyr::n()) %>% @@ -57,7 +59,7 @@ scatter_continent_year <- function(data, method = "lm", plotly = TRUE, ...) { length(unique(data$continent)), "Set2" )) - x <- rempsyc::nice_scatter( + p <- rempsyc::nice_scatter( data, predictor = "year", response = "papers_percentage", @@ -71,8 +73,32 @@ scatter_continent_year <- function(data, method = "lm", plotly = TRUE, ...) { if (isTRUE(plotly)) { insight::check_if_installed("plotly") - x <- plotly::ggplotly(tooltip = c("x", "y")) + p <- plotly::ggplotly(tooltip = c("x", "y")) + if (!is.null(citation)) { + p <- plotly_citation(p, citation, citation_size = citation_size) + } + } else if (!is.null(citation)) { + p <- gg_citation(p, citation, citation_size = citation_size) } - x + p +} + +plotly_citation <- function(x, citation, citation_size) { + plotly::layout( + x, + annotations = list( + # x = 2020, + y = 100, + text = citation2, + showarrow = F, + # xref = 'container', + yref = 'container', + xanchor = 'left', + yanchor = 'top', + # yshift = -1, + # automargin = TRUE, + # margin = list(b=90), + font = list(size = citation_size)) + ) } diff --git a/R/scatter_country_year.R b/R/scatter_country_year.R index 2691089..a910323 100644 --- a/R/scatter_country_year.R +++ b/R/scatter_country_year.R @@ -2,6 +2,8 @@ #' @param data The processed dataframe of data #' @param method Which method to use for the regression line, either "lm" (default) or "loess". #' @param plotly Logical, whether to use plotly for dynamic data visualization. +#' @param citation Optionally, a citation to add as a footer. +#' @param citation_size Font size of the citation. #' @param ... Further arguments passed to [rempsyc::nice_scatter] #' @examples #' \dontshow{ @@ -28,7 +30,7 @@ #' @importFrom rlang .data #' @export -scatter_country_year <- function(data, method = "lm", plotly = TRUE, ...) { +scatter_country_year <- function(data, method = "lm", plotly = TRUE, citation, citation_size = 15, ...) { df_country_year_missing <- data %>% dplyr::filter(is.na(.data$country)) %>% dplyr::group_by(.data$year) %>% @@ -55,7 +57,7 @@ scatter_country_year <- function(data, method = "lm", plotly = TRUE, ...) { getPalette <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(8, "Set2")) colours.country2 <- getPalette(length(unique(df_country_year$country))) - df_country_year %>% + p <- df_country_year %>% dplyr::mutate( year = as.numeric(.data$year), country = as.factor(.data$country) @@ -74,9 +76,14 @@ scatter_country_year <- function(data, method = "lm", plotly = TRUE, ...) { if (isTRUE(plotly)) { insight::check_if_installed("plotly") x <- plotly::ggplotly(tooltip = c("x", "y")) + if (!is.null(citation)) { + p <- plotly_citation(p, citation, citation_size = citation_size) + } + } else if (!is.null(citation)) { + p <- gg_citation(p, citation, citation_size = citation_size) } - x + p } #' @noRd diff --git a/R/waffle_continent.R b/R/waffle_continent.R index b1ad7e4..095c562 100644 --- a/R/waffle_continent.R +++ b/R/waffle_continent.R @@ -66,10 +66,3 @@ gg_citation <- function(x, citation, citation_size) { ggplot2::theme(axis.title.x = ggtext::element_markdown( hjust = 1, size = citation_size)) } - -plotly_citation <- function(x, citation) { - insight::check_if_installed("ggtext") - x + - ggplot2::xlab(citation) + - ggplot2::theme(axis.title.x = ggtext::element_markdown(hjust = 1)) -} diff --git a/man/scatter_continent_year.Rd b/man/scatter_continent_year.Rd index 299c71d..5f457c2 100644 --- a/man/scatter_continent_year.Rd +++ b/man/scatter_continent_year.Rd @@ -4,7 +4,14 @@ \alias{scatter_continent_year} \title{Generate table of journal paper percentages, by continent and year} \usage{ -scatter_continent_year(data, method = "lm", plotly = TRUE, ...) +scatter_continent_year( + data, + method = "lm", + plotly = TRUE, + citation, + citation_size = 15, + ... +) } \arguments{ \item{data}{The processed dataframe of data} @@ -13,6 +20,10 @@ scatter_continent_year(data, method = "lm", plotly = TRUE, ...) \item{plotly}{Logical, whether to use plotly for dynamic data visualization.} +\item{citation}{Optionally, a citation to add as a footer.} + +\item{citation_size}{Font size of the citation.} + \item{...}{Further arguments passed to \link[rempsyc:nice_scatter]{rempsyc::nice_scatter}} } \description{ diff --git a/man/scatter_country_year.Rd b/man/scatter_country_year.Rd index 53e5713..f15c017 100644 --- a/man/scatter_country_year.Rd +++ b/man/scatter_country_year.Rd @@ -4,7 +4,14 @@ \alias{scatter_country_year} \title{Generate table of journal paper percentages, by continent and year} \usage{ -scatter_country_year(data, method = "lm", plotly = TRUE, ...) +scatter_country_year( + data, + method = "lm", + plotly = TRUE, + citation, + citation_size = 15, + ... +) } \arguments{ \item{data}{The processed dataframe of data} @@ -13,6 +20,10 @@ scatter_country_year(data, method = "lm", plotly = TRUE, ...) \item{plotly}{Logical, whether to use plotly for dynamic data visualization.} +\item{citation}{Optionally, a citation to add as a footer.} + +\item{citation_size}{Font size of the citation.} + \item{...}{Further arguments passed to \link[rempsyc:nice_scatter]{rempsyc::nice_scatter}} } \description{