Skip to content

Commit

Permalink
add plotly_citation
Browse files Browse the repository at this point in the history
  • Loading branch information
rempsyc committed Apr 29, 2024
1 parent e739ab6 commit d8aa8f3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
34 changes: 30 additions & 4 deletions R/scatter_continent_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()) %>%
Expand Down Expand Up @@ -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",
Expand All @@ -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))
)
}
13 changes: 10 additions & 3 deletions R/scatter_country_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) %>%
Expand All @@ -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)
Expand All @@ -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
Expand Down
7 changes: 0 additions & 7 deletions R/waffle_continent.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
13 changes: 12 additions & 1 deletion man/scatter_continent_year.Rd

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

13 changes: 12 additions & 1 deletion man/scatter_country_year.Rd

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

0 comments on commit d8aa8f3

Please sign in to comment.