Skip to content

Commit

Permalink
update sort, add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbengler committed Jul 31, 2024
1 parent 9627378 commit 32bc195
Show file tree
Hide file tree
Showing 40 changed files with 471 additions and 788 deletions.
11 changes: 9 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ export(add_test_asterisks)
export(add_test_pvalue)
export(add_title)
export(add_violin)
export(adjust_caption)
export(adjust_colors)
export(adjust_description)
export(adjust_font)
export(adjust_legend)
export(adjust_legend_position)
export(adjust_legend_title)
export(adjust_padding)
export(adjust_size)
export(adjust_title)
export(adjust_x_axis)
export(adjust_x_axis_title)
export(adjust_y_axis)
export(adjust_y_axis_title)
export(all_rows)
export(as_tidyplot)
export(colors_continuous_bluepinkyellow)
Expand Down Expand Up @@ -105,9 +109,11 @@ export(last_rows)
export(max_rows)
export(min_rows)
export(new_color_scheme)
export(remove_caption)
export(remove_legend)
export(remove_legend_title)
export(remove_padding)
export(remove_title)
export(remove_x_axis)
export(remove_x_axis_labels)
export(remove_x_axis_line)
Expand Down Expand Up @@ -148,6 +154,7 @@ importFrom(lifecycle,deprecated)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(stats,density)
importFrom(stats,median)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(utils,tail)
2 changes: 1 addition & 1 deletion R/aaa.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @importFrom grDevices col2rgb rgb dev.off pdf
#' @importFrom stats density sd setNames
#' @importFrom stats density sd setNames median
#' @importFrom utils tail
#' @importFrom rlang := .data
NULL
Expand Down
17 changes: 9 additions & 8 deletions R/add-general.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param preserve Should dodging preserve the `"total"` width of all elements at
#' a position, or the width of a `"single"` element?
#' @param rasterize If `FALSE` (the default) the layer will be constructed of
#' vector shapes. If `TRUE` the layer will be rastered to a pixel image. This can
#' vector shapes. If `TRUE` the layer will be rasterized to a pixel image. This can
#' be useful when plotting many individual objects (1,000 or more) compromises
#' the performance of the generated PDF file.
#' @param rasterize_dpi The resolution in dots per inch (dpi) used for rastering
Expand Down Expand Up @@ -45,6 +45,7 @@
#' @param saturation A `number` between `0` and `1` for the color saturation of an object. A value of `0` is completely desaturated (white), `1` is the original color.
#' @param group Variable in the dataset to be used for grouping.
#' @param reverse Whether the order should be reversed or not. Defaults to `FALSE`, meaning not reversed.
#' @param .reverse Whether the order should be reversed or not. Defaults to `FALSE`, meaning not reversed.
#' @param scale_cut Scale cut function to be applied. See `scales::cut_short_scale()` and friends.
#' @param fontsize Font size in points. Defaults to `7`.
#' @param replace_na Whether to replace `count = NA` with `count = 0`.
Expand All @@ -61,7 +62,7 @@ NULL
## Error bar function factory
ff_errorbar <- function(.fun.data) {
function(plot, dodge_width = NULL, width = 0.4, linewidth = 0.25, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
position <- ggplot2::position_dodge(width = dodge_width, preserve = preserve)
plot + ggplot2::stat_summary(fun.data = .fun.data, geom = "errorbar",
Expand Down Expand Up @@ -130,7 +131,7 @@ add_ci95_errorbar <- ff_errorbar(.fun.data = mean_cl_boot)
## Ribbon function factory
ff_ribbon <- function(.fun.data) {
function(plot, dodge_width = NULL, alpha = 0.4, color = NA, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
mapping <- ggplot2::aes()
mapping$group <- plot$mapping$colour
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
Expand Down Expand Up @@ -195,7 +196,7 @@ add_ci95_ribbon <- ff_ribbon(.fun.data = ggplot2::mean_cl_boot)
## Bar function factory
ff_bar <- function(.fun, .count = FALSE) {
function(plot, dodge_width = NULL, width = 0.6, saturation = 1, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
position <- ggplot2::position_dodge(width = dodge_width, preserve = preserve)
if (saturation != 1) {
Expand All @@ -221,7 +222,7 @@ ff_bar <- function(.fun, .count = FALSE) {
## Dash function factory
ff_dash <- function(.fun, .count = FALSE) {
function(plot, dodge_width = NULL, width = 0.6, linewidth = 0.25, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
position <- ggplot2::position_dodge(width = dodge_width, preserve = preserve)
if (.count) {
Expand All @@ -237,7 +238,7 @@ ff_dash <- function(.fun, .count = FALSE) {
## Dot function factory
ff_dot <- function(.fun, .count = FALSE) {
function(plot, dodge_width = NULL, size = 2, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
position <- ggplot2::position_dodge(width = dodge_width, preserve = preserve)
if (.count) {
Expand All @@ -251,7 +252,7 @@ ff_dot <- function(.fun, .count = FALSE) {
ff_value <- function(.fun, .count = FALSE) {
function(plot, dodge_width = NULL, accuracy = 0.1, scale_cut = NULL, fontsize = 7,
extra_padding = 0.15, vjust = NULL, hjust = NULL, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
ptype <- get_plottype(plot)

if ((stringr::str_sub(ptype, 2, 2) == "c" || .count)) {
Expand Down Expand Up @@ -290,7 +291,7 @@ ff_value <- function(.fun, .count = FALSE) {
## Line function factory
ff_line <- function(.fun, .count = FALSE, .geom) {
function(plot, group, dodge_width = NULL, linewidth = 0.25, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
if(.geom == "area") linewidth = NA
mapping <- NULL
if (is_missing(plot, "group")) {
Expand Down
4 changes: 2 additions & 2 deletions R/add-heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @inherit common_arguments
#'
#' @details
#' * `add_heatmap()` supports rasterizing. See examples and [Advanced plotting](https://jbengler.github.io/tidyplots/articles/Advanced-plotting.html#rasterizing).
#' * `add_heatmap()` supports rasterization. See examples and [Advanced plotting](https://jbengler.github.io/tidyplots/articles/Advanced-plotting.html#rasterization).
#'
#' @examples
#' climate %>%
Expand All @@ -29,7 +29,7 @@
#' @export
add_heatmap <- function(plot, scale = c("none", "row", "column"), rotate_labels = 90,
rasterize = FALSE, rasterize_dpi = 300, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
mapping <- NULL
scale <- match.arg(scale)

Expand Down
26 changes: 13 additions & 13 deletions R/add-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
add_boxplot <- function(plot, dodge_width = NULL, saturation = 0.3, show_whiskers = TRUE, show_outliers = TRUE,
box_width = 0.6, whiskers_width = 0.8, outlier.size = 0.5, coef = 1.5,
outlier.shape = 19, linewidth = 0.25, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
position <- ggplot2::position_dodge(width = dodge_width, preserve = preserve)
if (saturation != 1) {
Expand Down Expand Up @@ -83,7 +83,7 @@ add_boxplot <- function(plot, dodge_width = NULL, saturation = 0.3, show_whisker
#' @export
add_violin <- function(plot, dodge_width = NULL, saturation = 0.3, draw_quantiles = NULL, trim = FALSE,
linewidth = 0.25, scale = "width", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
position <- ggplot2::position_dodge(width = dodge_width)
plot <- plot %>% adjust_colors(saturation = saturation)
Expand Down Expand Up @@ -114,7 +114,7 @@ add_violin <- function(plot, dodge_width = NULL, saturation = 0.3, draw_quantile
#'
#' @export
add_line <- function(plot, group, dodge_width = NULL, linewidth = 0.25, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
mapping <- NULL
if (is_missing(plot, "group")) {
mapping <- ggplot2::aes()
Expand All @@ -130,7 +130,7 @@ add_line <- function(plot, group, dodge_width = NULL, linewidth = 0.25, preserve
#' @rdname add_line
#' @export
add_area <- function(plot, group, dodge_width = NULL, linewidth = 0.25, alpha = 0.4, preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
ptype <- get_plottype(plot)

# detect orientation
Expand Down Expand Up @@ -192,7 +192,7 @@ add_area <- function(plot, group, dodge_width = NULL, linewidth = 0.25, alpha =
#' @export
add_curve_fit <- function(plot, dodge_width = NULL, method = "loess", linewidth = 0.25, alpha = 0.4,
preserve = "total", ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
mapping <- ggplot2::aes()
mapping$group <- plot$mapping$colour
dodge_width <- dodge_width %||% plot$tidyplot$dodge_width
Expand All @@ -219,15 +219,15 @@ add_curve_fit <- function(plot, dodge_width = NULL, method = "loess", linewidth
#'
#' @export
add_histogram <- function(plot, binwidth = NULL, bins = NULL, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
plot %>%
remove_padding(force_continuous = TRUE) +
ggplot2::geom_histogram(binwidth = binwidth, bins = bins, ...)
}
#' @rdname add_histogram
#' @export
add_density_histogram <- function(plot, binwidth = NULL, bins = NULL, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
plot %>%
remove_padding(force_continuous = TRUE) +
ggplot2::geom_histogram(ggplot2::aes(y = ggplot2::after_stat(density)),
Expand All @@ -236,7 +236,7 @@ add_density_histogram <- function(plot, binwidth = NULL, bins = NULL, ...) {
#' @rdname add_histogram
#' @export
add_density_curve <- function(plot, bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512, alpha = 0.4, color = "#D55E00",...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
plot %>%
remove_padding(force_continuous = TRUE) +
ggplot2::geom_density(bw = bw, adjust = adjust, kernel = kernel, n = n, color = color, fill = color, alpha = alpha, ...)
Expand Down Expand Up @@ -271,15 +271,15 @@ add_density_curve <- function(plot, bw = "nrd0", adjust = 1, kernel = "gaussian"
#'
#' @export
add_title <- function(plot, title = ggplot2::waiver()) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
# parse title
if (!is_waiver(title)) title <- tidyplot_parser(as.character(title))
plot + ggplot2::labs(title = title)
}
#' @rdname add_title
#' @export
add_caption <- function(plot, caption = ggplot2::waiver()) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
# parse caption
if (!is_waiver(caption)) caption <- tidyplot_parser(as.character(caption))
plot + ggplot2::labs(caption = caption)
Expand All @@ -306,7 +306,7 @@ add_caption <- function(plot, caption = ggplot2::waiver()) {
#'
#' @export
add_reference_lines <- function(plot, x = NULL, y = NULL, linetype = "dashed", linewidth = 0.25, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
out <- plot
if(!is.null(x)) {
out <- out + ggplot2::geom_vline(xintercept = x, linetype = linetype, linewidth = linewidth, ...)
Expand Down Expand Up @@ -377,7 +377,7 @@ add_reference_lines <- function(plot, x = NULL, y = NULL, linetype = "dashed", l
add_data_labels <- function(plot, label, data = all_rows(), fontsize = 7,
background = FALSE, background_color = "#FFFFFF", background_alpha = 0.6,
label_position = c("below", "above", "left", "right", "center"), ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
size <- fontsize/ggplot2::.pt
if (!background) background_alpha <- 0
label.padding <- ggplot2::unit(0.1, "lines")
Expand Down Expand Up @@ -414,7 +414,7 @@ add_data_labels <- function(plot, label, data = all_rows(), fontsize = 7,
add_data_labels_repel <- function(plot, label, data = all_rows(), fontsize = 7,
segment.size = 0.2, box.padding = 0.2, max.overlaps = Inf,
background = FALSE, background_color = "#FFFFFF", background_alpha = 0.6, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
size <- fontsize/ggplot2::.pt
if (!background) background_alpha <- 0
label.padding <- ggplot2::unit(0.1, "lines")
Expand Down
10 changes: 5 additions & 5 deletions R/add-points.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' * `add_data_points_beeswarm()` is based on `ggbeeswarm::geom_beeswarm()`.
#' Check there for additional arguments.
#'
#' * `add_data_points()` and friends support rasterizing. See examples and [Advanced plotting](https://jbengler.github.io/tidyplots/articles/Advanced-plotting.html#rasterizing).
#' * `add_data_points()` and friends support rasterization. See examples and [Advanced plotting](https://jbengler.github.io/tidyplots/articles/Advanced-plotting.html#rasterization).
#'
#' * `add_data_points()` and friends support data subsetting. See examples and [Advanced plotting](https://jbengler.github.io/tidyplots/articles/Advanced-plotting.html#data-subsetting).
#'
Expand Down Expand Up @@ -44,7 +44,7 @@
#' tidyplot(x = weight, y = size) %>%
#' add_data_points(alpha = 0.4)
#'
#' # Rasterizing
#' # Rasterization
#' animals %>%
#' tidyplot(x = weight, y = size) %>%
#' add_data_points(rasterize = TRUE, rasterize_dpi = 50)
Expand All @@ -61,7 +61,7 @@ add_data_points <- function(plot, data = all_rows(),
dodge_width = NULL,
preserve = "total",
rasterize = FALSE, rasterize_dpi = 300, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
f_points(plot = plot, data = data,
shape = shape, size = size, white_border = white_border,
dodge_width = dodge_width,
Expand All @@ -75,7 +75,7 @@ add_data_points_jitter <- function(plot, data = all_rows(),
dodge_width = NULL,
jitter_width = 0.2, jitter_height = 0, preserve = "total",
rasterize = FALSE, rasterize_dpi = 300, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
f_points(plot = plot, data = data,
shape = shape, size = size, white_border = white_border,
dodge_width = dodge_width,
Expand All @@ -90,7 +90,7 @@ add_data_points_beeswarm <- function(plot, data = all_rows(),
dodge_width = NULL,
preserve = "total",
rasterize = FALSE, rasterize_dpi = 300, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
f_points(beeswarm = TRUE,
plot = plot, data = data,
shape = shape, size = size, white_border = white_border,
Expand Down
6 changes: 3 additions & 3 deletions R/add-proportional.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Pie function factory
ff_pie <- function(.type = "pie") {
function(plot, width = 1, reverse = FALSE, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
plot <-
plot %>%
remove_padding() %>%
Expand Down Expand Up @@ -64,7 +64,7 @@ add_donut <- ff_pie(.type = "donut")
## Barstack function factory
ff_barstack <- function(.position_fun) {
function(plot, width = 0.8, reverse = FALSE, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
ptype <- get_plottype(plot)

if (is_missing(plot, "colour")) cli::cli_abort("Argument {.arg color} missing without default")
Expand Down Expand Up @@ -159,7 +159,7 @@ add_barstack_relative <- ff_barstack(.position_fun = ggplot2::position_fill)
## Areastack function factory
ff_areastack <- function(.position_fun) {
function(plot, linewidth = 0.25, alpha = 0.4, reverse = FALSE, replace_na = FALSE, ...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
ptype <- get_plottype(plot)

# overwrite group aesthetic
Expand Down
6 changes: 3 additions & 3 deletions R/add-stats.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Add statistics
#' Add statistical test
#' @param padding_top Extra padding above the data points to accommodate the statistical comparisons.
#' @param hide_info Whether to hide details about the statistical testing as caption. Defaults to `FALSE`.
#' @param ... Arguments passed on to `ggpubr::geom_pwc()`.
Expand Down Expand Up @@ -79,7 +79,7 @@ add_test_pvalue <- function(plot,
),
hide_info = FALSE,
...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
# cli::cli_alert_success("add_test: {.pkg method} = {method}, {.pkg label} = {label}, {.pkg p.adjust.method} = {p.adjust.method}, {.pkg hide.ns} = {hide.ns}")

plot <- plot %>%
Expand Down Expand Up @@ -125,7 +125,7 @@ add_test_asterisks <- function(plot,
),
hide_info = FALSE,
...) {
check_tidyplot(plot)
plot <- check_tidyplot(plot)
add_test_pvalue(plot,
padding_top = padding_top,
method = method,
Expand Down
Loading

0 comments on commit 32bc195

Please sign in to comment.