diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml new file mode 100644 index 00000000..14159b77 --- /dev/null +++ b/.github/workflows/R-CMD-check.yaml @@ -0,0 +1,50 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +name: R-CMD-check + +jobs: + R-CMD-check: + runs-on: ${{ matrix.config.os }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + - {os: macos-latest, r: 'release'} + - {os: windows-latest, r: 'release'} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + - {os: ubuntu-latest, r: 'oldrel-1'} + + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r@v2 + with: + r-version: ${{ matrix.config.r }} + http-user-agent: ${{ matrix.config.http-user-agent }} + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::rcmdcheck + needs: check + + - uses: r-lib/actions/check-r-package@v2 + with: + upload-snapshots: true + build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' diff --git a/DESCRIPTION b/DESCRIPTION index 0f64a1ff..5f61194c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tidyplots Title: Tidy Plots Using the Pipe -Version: 0.0.2 +Version: 0.0.2.9000 Authors@R: person("Jan Broder", "Engler", , "broder.engler@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0002-3169-2076")) @@ -11,7 +11,6 @@ Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 Imports: cli, - colorspace, dplyr, forcats, ggbeeswarm, @@ -21,6 +20,7 @@ Imports: ggrepel, glue, Hmisc, + htmltools, patchwork, purrr, ragg, @@ -28,7 +28,6 @@ Imports: scales, stringr, svglite, - tibble, tidyr Depends: R (>= 2.10) diff --git a/NAMESPACE b/NAMESPACE index 37187436..5600369f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,9 @@ # Generated by roxygen2: do not edit by hand +S3method("[",tidycolor) +S3method("[[",tidycolor) +S3method(c,tidycolor) +S3method(print,tidycolor) export("%>%") export(add) export(add_area) @@ -68,6 +72,17 @@ export(adjust_rotate_plot) export(adjust_x_axis) export(adjust_y_axis) export(all_rows) +export(colors_continuous_blue_pink_yellow) +export(colors_continuous_inferno) +export(colors_continuous_magma) +export(colors_continuous_turbo) +export(colors_continuous_viridis) +export(colors_discrete_circle) +export(colors_discrete_metro_ui) +export(colors_diverging_RdBu) +export(colors_diverging_RdYlBu) +export(colors_diverging_blue2brown) +export(colors_diverging_blue2red) export(filter_rows) export(first_rows) export(format_number) @@ -75,6 +90,7 @@ export(format_p_value) export(last_rows) export(max_rows) export(min_rows) +export(new_color_scheme) export(remove_legend) export(remove_legend_title) export(remove_padding) @@ -97,9 +113,8 @@ export(reorder_y_axis_labels) export(reverse_color_labels) export(reverse_x_axis_labels) export(reverse_y_axis_labels) +export(sample_rows) export(save_plot) -export(show_color_palettes) -export(show_plot) export(sort_color_labels) export(sort_x_axis_labels) export(sort_y_axis_labels) @@ -110,3 +125,4 @@ export(theme_minimal_xy) export(theme_minimal_y) export(theme_tidyplot) export(tidyplot) +export(view_plot) diff --git a/NEWS.md b/NEWS.md index 77ea4d8c..cbe85904 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,11 +1,17 @@ +# tidyplots (development version) + +* New S3 class `tidycolor` for color schemes. The print method of `tidycolor` sends a html preview of the color scheme to the RStudio viewer panel. +* New `new_color_scheme()` to create custom color schemes. +* New build-in color schemes using the prefix `colors_discrete_`, `colors_continuous_` and `colors_diverging_`. +* `adjust_colors()` now also works with too few or too many provided colors. +* New function factory behind `adjust_x_axis()` and `adjust_y_axis()` + # tidyplots 0.0.2 * Renaming functions to improve consistency. - * The package is still in early development. Expect user-facing and breaking changes. # tidyplots 0.0.1 * Initial release. tidyplots streamlines the creation of publication-ready plots for scientific papers, making it incredibly easy to add and refine plot elements. It allows precise control over composition, style, and absolute sizes, while its utilization of the pipe `%>%` simplifies the construction of advanced plotting pipelines. - * The package is still in early development. Expect user-facing and breaking changes. diff --git a/R/aaa.R b/R/aaa.R new file mode 100644 index 00000000..67b6223f --- /dev/null +++ b/R/aaa.R @@ -0,0 +1,59 @@ +color_palettes <- list( + black_blue_pink_yellow = c( + "#161523","#171943","#2C2E68","#2E3D88","#3F519D","#48569F","#5F569F", + "#7D5099","#8B5197","#A14F96","#C85296","#DB5999","#E06A7F","#E27F6D", + "#E68F63","#ECA562","#F3BD66","#F9D966","#FAF06F","#EEE969"), + + blue_pink_yellow = c( + "#00034D","#000F9F","#001CEF","#241EF5","#5823F6","#A033E0","#E85AB1", + "#F1907C","#F4AF63","#FCE552","#FFFB6D"), + + blue2brown = c( + "#1961A5","#2671B5","#2D80BF","#268CC9","#119DD8","#00B2EB","#66C5EF", + "#C4E5F8","#FEFCF6","#FDEEB8","#FCDD67","#F6C445","#E78B43","#DD5642", + "#DB3E34","#CA3632","#B3322E"), + + bunq = c( + "#248746","#2D9B47","#62B64F","#88CC53","#3CB7AA","#3296D7","#2772BA", + "#1D5D83","#973431","#E23133","#F18823","#F5C836"), + + color_circle_vivid = c( + "#939393","#644296","#F08533","#D1352C","#559E3F","#3B78B0"), + + ggsci_d3 = c( + "#1F77B4","#FF7F0E","#2CA02C","#D62728","#9467BD","#8C564B","#E377C2", + "#7F7F7F","#BCBD22","#17BECF"), + + ggsci_jco = c( + "#0073C2","#EFC000","#868686","#CD534C","#7AA6DC","#003C67","#8F7700", + "#3B3B3B","#A73030","#4A6990"), + + ggsci_locuszoom = c( + "#D43F3A","#EEA236","#5CB85C","#46B8DA","#357EBD","#9632B8","#B8B8B8"), + + ggsci_npg = c( + "#E64B35","#4DBBD5","#00A087","#3C5488","#F39B7F","#8491B4","#91D1C2", + "#DC0000","#7E6148","#B09C85"), + + metro_ui = c( + "#4DACD6","#4FAE62","#F6C54D","#E37D46","#C02D45"), + + multiqc_align = c( + "#437BB1","#7CB5EC","#F7A35C","#B1084C","#7F0000"), + + multiqc_count = c( + "#7CB5EC","#434348","#90ED7D","#F7A35C"), + + multiqc_fastq_screen = c( + "#87B5E7","#434348","#A6E98A","#ECA669","#8087E2","#E06681","#E2D269", + "#F7A35C","#B1084C","#4A8E8E","#E36561","#77150C","#CCCCCC"), + + white_ocre_brown = c( + "#FDFCF4","#F9F0CC","#F9E59F","#F4DA7A","#F1CB62","#EDBF55","#DF9350", + "#D87D4A","#CF5C45","#BC403D","#AD3C39","#9F3736","#983534","#842E2E"), + + white_turquise_blue = c( + "#FCFBDF","#F7F7D1","#EDF0BD","#E3EBB6","#CFE1B5","#C4DDB6","#9FCEB4", + "#83C5B3","#5DBAB7","#1FB1BA","#00AFBF","#049FBF","#0493BF","#037DB5", + "#036DAC","#16579E","#224B95","#2B3D8B","#2B3977","#232959","#1F2451") +) diff --git a/R/add.R b/R/add.R index 24780554..69b8a05f 100644 --- a/R/add.R +++ b/R/add.R @@ -761,7 +761,7 @@ add_heatmap <- function(gg, scale = c("none", "row", "column"), rotate_labels = ggplot2::coord_cartesian(expand = FALSE) if (scale %in% c("row", "column")) - gg <- gg %>% adjust_colors(c("blue", "white", "red"), as_palette = TRUE) + gg <- gg %>% adjust_colors(c("blue", "white", "red")) add_geom(gg, ggplot2::geom_raster(mapping = mapping, ...), rasterize = rasterize, rasterize_dpi = rasterize_dpi) diff --git a/R/adjust.R b/R/adjust.R index 046c1880..bf7795ae 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -130,45 +130,6 @@ adjust_x_axis <- ff_adjust_axis("x") adjust_y_axis <- ff_adjust_axis("y") -#' Adjust colors -#' @param gg bla -#' @param new_colors bla -#' @param saturation bla -#' @param as_palette bla -#' @param labels bla -#' @param ... bla -#' @export -adjust_colors <- function(gg, new_colors = NULL, saturation = 1, as_palette = FALSE, - labels = tidyplot_parse_labels(), ...) { - out <- gg - # as individual colors - if (!is.null(new_colors) && as_palette == FALSE) { - out$tidyplot$new_colors <- new_colors - suppressMessages( - out <- - out + - ggplot2::scale_fill_manual(values = apply_saturation(new_colors, saturation = saturation), drop = FALSE, labels = labels, ...) + - ggplot2::scale_color_manual(values = new_colors, drop = FALSE, labels = labels, ...) - ) - cli::cli_alert_success("adjust_colors: applied {.pkg new_colors}") - } else { - # as color palette - suppressMessages({ - if (is_discrete(gg, "colour")) - out <- out + my_scale_color_d(palette = new_colors, drop = FALSE, labels = labels, ...) - if (is_discrete(gg, "fill")) - out <- out + my_scale_fill_d(palette = new_colors, saturation = saturation, drop = FALSE, labels = labels, ...) - if (is_continuous(gg, "colour")) - out <- out + my_scale_color_c(palette = new_colors, labels = labels, ...) - if (is_continuous(gg, "fill")) - out <- out + my_scale_fill_c(palette = new_colors, saturation = saturation, labels = labels, ...) - }) - cli::cli_alert_success("adjust_colors: applied {.pkg new color palette}") - } - out -} - - #' Adjust plot size #' @param gg bla #' @param width bla diff --git a/R/colors.R b/R/colors.R index b47aaf67..9fc901c3 100644 --- a/R/colors.R +++ b/R/colors.R @@ -1,36 +1,174 @@ +#' @export +print.tidycolor <- function(x, return_html = FALSE, ...) print_tidycolor(x, return_html = return_html) +#' @export +c.tidycolor <- function(...) new_color_scheme(NextMethod()) +#' @export +`[.tidycolor` <- function(x, i) new_color_scheme(NextMethod()) +#' @export +`[[.tidycolor` <- function(x, i) new_color_scheme(NextMethod()) + +#' Adjust colors +#' @param gg bla +#' @param new_colors bla +#' @param saturation bla +#' @param labels bla +#' @param ... bla +#' @export +adjust_colors <- function(gg, new_colors = NULL, + saturation = 1, + labels = tidyplot_parse_labels(), ...) { + out <- gg + as_palette <- FALSE + + if (is_discrete(gg, "colour")) { + + # Default colors + if (is.null(new_colors)) { + new_colors <- colors_discrete_metro_ui + as_palette <- TRUE + } + # Are enough new_colors provided? + named_vector <- FALSE + n_ratio <- 0 + if (!is.null(names(new_colors))) { + named_vector <- TRUE + n_ratio <- 1 + } else { + n_provided <- length(new_colors) + n_requested <- + dplyr::pull(gg$data, get_variable(gg, "colour")) %>% + unique() %>% + length + n_ratio <- n_provided / n_requested + } + + if (named_vector || !as_palette && n_ratio >= 1) { + + # Too many colors + if (n_ratio > 1 && !as_palette) { + cli::cli_alert_danger("adjust_colors: Too many colors. {n_provided} colors provided, but only {n_requested} needed.") + new_colors <- downsample_vector(new_colors, n_requested) + } + + suppressMessages(out <- out + ggplot2::scale_color_manual(values = new_colors, drop = FALSE, labels = labels, ...)) + suppressMessages(out <- out + ggplot2::scale_fill_manual(values = apply_saturation(new_colors, saturation = saturation), drop = FALSE, labels = labels, ...)) + cli::cli_alert_success("adjust_colors: applied discrete {.pkg color values}") + + } else { + suppressMessages(out <- out + scale_color_d(palette = new_colors, drop = FALSE, labels = labels, ...)) + suppressMessages(out <- out + scale_fill_d(palette = new_colors, saturation = saturation, drop = FALSE, labels = labels, ...)) + cli::cli_alert_success("adjust_colors: applied discrete {.pkg color palette}") + + # Too few colors + if (n_ratio < 1 && !as_palette) cli::cli_alert_danger("adjust_colors: Too few colors. {n_provided} colors provided, but {n_requested} expected.") + } + } + + if (is_continuous(gg, "colour")) { + # Default colors + if (is.null(new_colors)) new_colors <- colors_continuous_blue_pink_yellow + + suppressMessages(out <- out + scale_color_c(palette = new_colors, labels = labels, ...)) + suppressMessages(out <- out + scale_fill_c(palette = new_colors, saturation = saturation, labels = labels, ...)) + cli::cli_alert_success("adjust_colors: applied continous {.pkg color palette}") + } + out +} -#' Show color palettes +#' New color scheme #' -#' @param pals bla -#' @param show_labels bla -#' @param ncol bla -#' @param ... bla +#' @param x bla +#' @param name bla #' @export -show_color_palettes <- function(pals = color_palettes, show_labels = TRUE, ncol = 3, ...) { - tidy_pals <- - tibble::enframe(pals, name = "pal") %>% - tidyr::unnest(cols = c(value)) %>% - tibble::rownames_to_column("nr") %>% - dplyr::mutate( - nr = factor(nr, levels = dplyr::row_number()), - label_color = dplyr::if_else(as(colorspace::hex2RGB(value), "HLS")@coords[,2] > 0.6, "#000000", "#FFFFFF")) - - fill_colors <- tibble::deframe(tidy_pals[c(3,3)]) - label_colors <- tibble::deframe(tidy_pals[c(4,4)]) - - ggplot2::ggplot(tidy_pals, ggplot2::aes(x = nr, y = 1, fill = value)) + - ggplot2::geom_tile() + - { if (show_labels) ggplot2::geom_text(ggplot2::aes(label = value, color = label_color), angle = 90, size = 2.5) else NULL - } + - ggplot2::scale_x_discrete(expand = c(0, 0)) + - ggplot2::scale_fill_manual(values = fill_colors, guide = "none") + - ggplot2::scale_color_manual(values = label_colors, guide = "none") + - ggplot2::theme_void() + - ggplot2::facet_wrap(facets = ggplot2::vars(pal), scales = "free", ncol = ncol, ...) +new_color_scheme <- function(x, name = "Untitled color scheme") { + x <- as.character(x) + structure(x, class = c('tidycolor', 'character'), tidycolor.name = name) } + +#' Discrete color schemes +#' +#' @details +#' Previews of build-in discrete color schemes +#' +#' ```{r results="asis", echo=FALSE, message=FALSE} +#' print(colors_discrete_metro_ui, return_html = TRUE) +#' print(colors_discrete_circle, return_html = TRUE) +#' ``` +#' @md +#' +#' @export +colors_discrete_metro_ui <- new_color_scheme(color_palettes$metro_ui, "colors_discrete_metro_ui") +#' @rdname colors_discrete_metro_ui +#' @export +colors_discrete_circle <- new_color_scheme(color_palettes$color_circle_vivid, "colors_discrete_circle") + + +#' Continuous color schemes +#' +#' @details +#' Previews of build-in continuous color schemes +#' +#' ```{r results="asis", echo=FALSE, message=FALSE} +#' print(colors_continuous_viridis, return_html = TRUE) +#' print(colors_continuous_magma, return_html = TRUE) +#' print(colors_continuous_inferno, return_html = TRUE) +#' print(colors_continuous_turbo, return_html = TRUE) +#' print(colors_continuous_blue_pink_yellow, return_html = TRUE) +#' ``` +#' @md +#' +#' @export +colors_continuous_viridis <- new_color_scheme(viridisLite::viridis(265), name = "colors_continuous_viridis") +#' @rdname colors_continuous_viridis +#' @export +colors_continuous_magma <- new_color_scheme(viridisLite::magma(265), "colors_continuous_magma") +#' @rdname colors_continuous_viridis +#' @export +colors_continuous_inferno <- new_color_scheme(viridisLite::inferno(265),"colors_continuous_inferno") +#' @rdname colors_continuous_viridis +#' @export +colors_continuous_turbo <- new_color_scheme(viridisLite::turbo(265), "colors_continuous_turbo") +#' @rdname colors_continuous_viridis +#' @export +colors_continuous_blue_pink_yellow <- new_color_scheme(color_palettes$blue_pink_yellow, "colors_continuous_blue_pink_yellow") + + +#' Diverging color schemes +#' +#' @details +#' Previews of build-in diverging color schemes +#' +#' ```{r results="asis", echo=FALSE, message=FALSE} +#' print(colors_diverging_blue2brown, return_html = TRUE) +#' print(colors_diverging_blue2red, return_html = TRUE) +#' print(colors_diverging_RdBu, return_html = TRUE) +#' print(colors_diverging_RdYlBu, return_html = TRUE) +#' ``` +#' @md +#' +#' @export +colors_diverging_blue2brown <- new_color_scheme(color_palettes$blue2brown, "colors_diverging_blue2brown") +#' @rdname colors_diverging_blue2brown +#' @export +colors_diverging_blue2red <- new_color_scheme(c("#0000FF","#FFFFFF","#FF0000"), "colors_diverging_blue2red") +#' @rdname colors_diverging_blue2brown +#' @export +colors_diverging_RdBu <- new_color_scheme(RColorBrewer::brewer.pal("RdBu", n = 11), "colors_diverging_RdBu") +#' @rdname colors_diverging_blue2brown +#' @export +colors_diverging_RdYlBu <- new_color_scheme(RColorBrewer::brewer.pal("RdYlBu", n = 9), "colors_diverging_RdYlBu") + + # not exported +downsample_vector <- function(x, n) { + if (length(x) <= n) return(x) + by <- (length(x) / (n-1)) - (1 / (n-1)) + i <- round(cumsum(c(1, rep(by, n-1)))) + x[i] +} + apply_saturation <- function(colors, saturation, background_color = "#FFFFFF") { purrr::map_chr(colors, function(color) { color <- col2rgb(color) @@ -40,33 +178,80 @@ apply_saturation <- function(colors, saturation, background_color = "#FFFFFF") { }) } -my_pal <- function(palette, reverse = FALSE, saturation = 1, ...) { +generate_html <- function(x, max_colors = 64) { + name <- attr(x, "tidycolor.name") + size <- length(x) + downsampled <- FALSE + downsample_text <- "" + + if (length(x) > max_colors) { + downsampled <- TRUE + downsample_text <- paste0(", downsampled to ",max_colors," colors") + x <- downsample_vector(x, max_colors) + } + + color_bar <- color_vector <- paste0("", collapse = "") + color_vector <- paste0("\"",x,"\"", collapse = ",") + + paste0(" +
+

", name, "

+ A tidyplots color scheme with ",size," colors",downsample_text,". +

+ ",color_bar," +

+ c(
", color_vector ,") +

") +} + +print_tidycolor <- function(x, return_html = FALSE, max_colors = 64) { + cli::cli_alert_info(paste0("A tidyplots color scheme with ",length(x) ," colors.")) + + viewer <- getOption("viewer") + if (!is.null(viewer) || return_html) { + + inner_html <- generate_html(x = x, max_colors = max_colors) + if (return_html) + return(htmltools::HTML(inner_html)) + + tempDir <- tempfile() + dir.create(tempDir) + htmlFile <- file.path(tempDir, "index.html") + html <- paste0(" + ", inner_html, "") + + writeLines(html, htmlFile) + viewer(htmlFile) + cli::cli_alert_success("A preview was send to the RStudio viewer pane.") + } else { + print(as.character(x)) + } +} + +# palette and scale functions +make_palette <- function(palette, reverse = FALSE, saturation = 1, ...) { pal <- palette if (reverse) pal <- rev(pal) pal <- apply_saturation(pal, saturation = saturation) grDevices::colorRampPalette(pal, ...) } -my_scale_color_c <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { - if (is.null(palette)) palette <- color_palettes[["blue_pink_yellow"]] - pal <- my_pal(palette = palette, saturation = saturation, reverse = reverse) - ggplot2::scale_color_gradientn(colours = pal(256), ...) +scale_color_d <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { + pal <- make_palette(palette = palette, saturation = saturation, reverse = reverse) + ggplot2::discrete_scale("colour", paste0("my_", palette), palette = pal, ...) } -my_scale_color_d <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { - if (is.null(palette)) palette <- color_palettes[["metro_ui"]] - pal <- my_pal(palette = palette, saturation = saturation, reverse = reverse) - ggplot2::discrete_scale("colour", paste0("my_", palette), palette = pal, ...) +scale_fill_d <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { + pal <- make_palette(palette = palette, saturation = saturation, reverse = reverse) + ggplot2::discrete_scale("fill", paste0("my_", palette), palette = pal, ...) } -my_scale_fill_c <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { - if (is.null(palette)) palette <- color_palettes[["blue_pink_yellow"]] - pal <- my_pal(palette = palette, saturation = saturation, reverse = reverse) - ggplot2::scale_fill_gradientn(colours = pal(256), ...) +scale_color_c <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { + pal <- make_palette(palette = palette, saturation = saturation, reverse = reverse) + ggplot2::scale_color_gradientn(colours = pal(256), ...) } -my_scale_fill_d <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { - if (is.null(palette)) palette <- color_palettes[["metro_ui"]] - pal <- my_pal(palette = palette, saturation = saturation, reverse = reverse) - ggplot2::discrete_scale("fill", paste0("my_", palette), palette = pal, ...) +scale_fill_c <- function(palette = NULL, saturation = 1, reverse = FALSE, ...) { + pal <- make_palette(palette = palette, saturation = saturation, reverse = reverse) + ggplot2::scale_fill_gradientn(colours = pal(256), ...) } diff --git a/R/data.R b/R/data.R index d3372ea6..fd4dcf02 100644 --- a/R/data.R +++ b/R/data.R @@ -88,13 +88,3 @@ #' dplyr::glimpse(eu_countries) "eu_countries" -#' Color palettes -#' @format A list of 18 color palettes. -#' @source -#' `tidyplots` package -#' @examples -#' color_palettes -"color_palettes" - - - diff --git a/R/helpers.R b/R/helpers.R index de438c1e..98db8ff5 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -37,15 +37,13 @@ filter_rows <- function(..., .by = NULL){ #' @inheritParams dplyr::slice_max #' @export max_rows <- function(order_by, n, by = NULL, with_ties = TRUE, na_rm = FALSE){ - . %>% dplyr::slice_max(order_by = {{order_by}}, n = n, by = {{by}}, with_ties = with_ties, - na_rm = na_rm) + . %>% dplyr::slice_max(order_by = {{order_by}}, n = n, by = {{by}}, with_ties = with_ties, na_rm = na_rm) } #' @rdname all_rows #' @inheritParams dplyr::slice_min #' @export min_rows <- function(order_by, n, by = NULL, with_ties = TRUE, na_rm = FALSE){ - . %>% dplyr::slice_min(order_by = {{order_by}}, n = n, by = {{by}}, with_ties = with_ties, - na_rm = na_rm) + . %>% dplyr::slice_min(order_by = {{order_by}}, n = n, by = {{by}}, with_ties = with_ties, na_rm = na_rm) } #' @rdname all_rows #' @inheritParams dplyr::slice_head @@ -59,6 +57,12 @@ first_rows <- function(n, by = NULL){ last_rows <- function(n, by = NULL){ . %>% dplyr::slice_tail(n = n, by = {{by}}) } +#' @rdname all_rows +#' @inheritParams dplyr::slice_sample +#' @export +sample_rows <- function(n, by = NULL){ + . %>% dplyr::slice_sample(n = n, by = {{by}}) +} #' Format numbers and p values @@ -102,7 +106,7 @@ check_pipeline <- function(gg) { # add, adjust, theme, remove, split, render, save # add before adjust -> warn # adjust_levels before adjust_colors -> warn - # nothing after split_plot, except save_plot and show_plot -> error + # nothing after split_plot, except save_plot and view_plot -> error # unexpected behavior: adjust_levels(new_names) + adjust_colors(new_colors = named_vector) } diff --git a/R/plot.R b/R/plot.R index bbf93f96..8c2bce7b 100644 --- a/R/plot.R +++ b/R/plot.R @@ -53,14 +53,14 @@ tidyplot <- function(data, ..., width = 50, height = 50, dodge_width = 0.8) { # add_median_dash() %>% # add_data_points() -#' Show plot on screen +#' View plot on screen #' #' @param gg bla #' @param data bla #' @param title bla #' @param ... bla #' @export -show_plot <- function(gg, data = all_rows(), title = ggplot2::waiver(), ...) { +view_plot <- function(gg, data = all_rows(), title = ggplot2::waiver(), ...) { input <- gg if (inherits(data, "function")) gg <- gg %+% (gg$data %>% data()) + ggplot2::ggtitle(title) if (inherits(data, "data.frame")) gg <- gg %+% data + ggplot2::ggtitle(title) @@ -68,8 +68,8 @@ show_plot <- function(gg, data = all_rows(), title = ggplot2::waiver(), ...) { invisible(input) } -# gg %>% show_plot(data = filter_rows(sex == "female"), title = "bla") -# gg %>% show_plot(data = study %>% dplyr::slice_sample(n = 4), title = "bla") +# gg %>% view_plot(data = filter_rows(sex == "female"), title = "bla") +# gg %>% view_plot(data = study %>% dplyr::slice_sample(n = 4), title = "bla") multipage_plots <- function(gg, ncol = NULL, diff --git a/README.Rmd b/README.Rmd index 05e4865a..97afa0e8 100644 --- a/README.Rmd +++ b/README.Rmd @@ -22,6 +22,7 @@ knitr::opts_chunk$set( # tidyplots tidyplots website +[![R-CMD-check](https://github.com/jbengler/tidyplots/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/jbengler/tidyplots/actions/workflows/R-CMD-check.yaml) tidyplots streamlines the creation of publication-ready plots for scientific papers, making it incredibly easy to add and refine plot elements. It allows precise control over composition, style, and absolute sizes, while its utilization of the pipe `%>%` simplifies the construction of advanced plotting pipelines. @@ -123,11 +124,11 @@ study %>% add_mean_bar(alpha = 0.3) %>% add_error() %>% add_data_points_beeswarm() %>% - show_plot(title = "Plot version A") %>% + view_plot(title = "Plot version A") %>% adjust_rotate_plot() %>% - show_plot(title = "Plot version B") %>% - adjust_font(family = "mono", fontsize = 10, face = "bold") %>% - show_plot(title = "Plot version C") + view_plot(title = "Plot version B") %>% + adjust_font(family = "mono", face = "bold") %>% + view_plot(title = "Plot version C") ``` diff --git a/README.md b/README.md index 47633937..0e706826 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ # tidyplots tidyplots website + +[![R-CMD-check](https://github.com/jbengler/tidyplots/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/jbengler/tidyplots/actions/workflows/R-CMD-check.yaml) tidyplots streamlines the creation of publication-ready plots for @@ -120,11 +122,11 @@ study %>% add_mean_bar(alpha = 0.3) %>% add_error() %>% add_data_points_beeswarm() %>% - show_plot(title = "Plot version A") %>% + view_plot(title = "Plot version A") %>% adjust_rotate_plot() %>% - show_plot(title = "Plot version B") %>% - adjust_font(family = "mono", fontsize = 10, face = "bold") %>% - show_plot(title = "Plot version C") + view_plot(title = "Plot version B") %>% + adjust_font(family = "mono", face = "bold") %>% + view_plot(title = "Plot version C") ``` diff --git a/_pkgdown.yml b/_pkgdown.yml index 681e112e..a5333552 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -96,6 +96,13 @@ reference: - contents: - starts_with("theme") - starts_with("remove") +- subtitle: + desc: > + **Colors** +- contents: + - starts_with("colors_discrete") + - starts_with("colors_continuous") + - starts_with("colors_diverging") - title: Split desc: Split the plot into a multi plot layout, eventually across multiple pages. - contents: @@ -103,15 +110,14 @@ reference: - title: Output desc: Output the final plot or intermediate stages to the screen or to a file. - contents: - - show_plot + - view_plot - save_plot - title: Helpers - contents: - add - - starts_with("format") + - starts_with("format_") - ends_with("_rows") - - starts_with("color_palettes") - - starts_with("show_color_palettes") + - starts_with("new_color_scheme") - title: Data - contents: - starts_with("animals") diff --git a/data-raw/color_palettes.R b/data-raw/color_palettes.R deleted file mode 100644 index f3e1a62e..00000000 --- a/data-raw/color_palettes.R +++ /dev/null @@ -1,8 +0,0 @@ -## code to prepare `color_palettes` dataset goes here - -library(tidyverse) - -load("data-raw/color_palettes.rda") -color_palettes <- map(color_palettes, unname) - -usethis::use_data(color_palettes, overwrite = TRUE) diff --git a/data-raw/color_palettes.rda b/data-raw/color_palettes.rda deleted file mode 100644 index 8db0863a..00000000 Binary files a/data-raw/color_palettes.rda and /dev/null differ diff --git a/data-raw/old_stuff.R b/data-raw/old_stuff.R new file mode 100644 index 00000000..849526ee --- /dev/null +++ b/data-raw/old_stuff.R @@ -0,0 +1,33 @@ + + +print_color <- function(hex, show_hex_code = TRUE) { + bg_style <- cli::make_ansi_style(hex, bg = TRUE) + text_color <- dplyr::if_else(as(colorspace::hex2RGB(hex), "HLS")@coords[,2] > 0.6, "#000000", "#FFFFFF") + text_style <- cli::make_ansi_style(text_color) + if (show_hex_code) + cat(bg_style(text_style(paste0('"',hex,'"')))) + else + cat(bg_style(text_style(paste0(' ')))) +} + +preview_colors_in_console <- function(x, max_colors = 64) { + if (length(x) > max_colors) { + cat(paste0(length(x), " colors downsampled to ",max_colors," colors:\n")) + x <- downsample_vector(x, max_colors) + } + + n_per_line <- floor(cli::console_width() / 3) + for (i in 1:length(x)) { + print_color(x[i], show_hex_code = FALSE) + if (i %% n_per_line == 0 && i != length(x)) cat("\n") + } + n_per_line <- floor(cli::console_width() / (nchar(x[[1]]) + 3)) + cat("\n\nc(") + for (i in 1:length(x)) { + print_color(x[i]) + if (i != length(x)) cat(",") + if (i %% n_per_line == 0 && i != length(x)) cat("\n") + } + cat(")\n\nDisclaimer: Color preview in the console is not accurate.\nUse `view()` for accurate color preview.") + cat("\n") +} diff --git a/data-raw/testing.R b/data-raw/testing.R index b4dd3c45..3c644437 100644 --- a/data-raw/testing.R +++ b/data-raw/testing.R @@ -269,7 +269,7 @@ gene_expression %>% adjust_x_axis(rotate_labels = 90) %>% adjust_data_labels(external_gene_name, sort_by = -dplyr::desc(direction)) %>% adjust_data_labels(direction, sort_by = dplyr::desc(direction)) %>% - adjust_colors(c("blue", "white", "red"), as_palette = TRUE) %>% + adjust_colors(c("blue", "white", "red")) %>% adjust_plot_size(height = 90) h1 <- @@ -281,7 +281,7 @@ h1 <- adjust_x_axis(rotate_labels = 90) %>% adjust_data_labels(external_gene_name, sort_by = -dplyr::desc(direction)) %>% adjust_data_labels(direction, sort_by = dplyr::desc(direction)) %>% - adjust_colors(c("blue", "white", "red"), as_palette = TRUE) %>% + adjust_colors(c("blue", "white", "red")) %>% adjust_plot_size(height = 90) h1 @@ -293,7 +293,7 @@ h2 <- h1 + h2 %>% split_plot(sample_type, heights = 90) h3 <- h1 %>% - adjust_colors(c("blue", "white", "red"), as_palette = TRUE, limits = c(-3, 3)) + + adjust_colors(c("blue", "white", "red"), limits = c(-3, 3)) + ggplot2::facet_grid(cols = dplyr::vars(condition), rows = dplyr::vars(direction), scales = "free_y") h3 %>% split_plot(sample_type, heights = 90) diff --git a/data-raw/time_course.R b/data-raw/time_course.R index de8bda67..25377737 100644 --- a/data-raw/time_course.R +++ b/data-raw/time_course.R @@ -24,7 +24,6 @@ time_course %>% tidyplot(x = day, y = subject, color = score, height = 80) %>% add_heatmap(rotate_labels = FALSE) %>% adjust_colors(new_colors = rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")), - as_palette = TRUE, na.value = "#DDDDDD") + facet_grid(vars(treatment), scales = "free") diff --git a/data/color_palettes.rda b/data/color_palettes.rda deleted file mode 100644 index ce0a1e24..00000000 Binary files a/data/color_palettes.rda and /dev/null differ diff --git a/man/adjust_colors.Rd b/man/adjust_colors.Rd index 21a85afe..f5705205 100644 --- a/man/adjust_colors.Rd +++ b/man/adjust_colors.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/adjust.R +% Please edit documentation in R/colors.R \name{adjust_colors} \alias{adjust_colors} \title{Adjust colors} @@ -8,7 +8,6 @@ adjust_colors( gg, new_colors = NULL, saturation = 1, - as_palette = FALSE, labels = tidyplot_parse_labels(), ... ) @@ -20,8 +19,6 @@ adjust_colors( \item{saturation}{bla} -\item{as_palette}{bla} - \item{labels}{bla} \item{...}{bla} diff --git a/man/all_rows.Rd b/man/all_rows.Rd index 25ac66ca..10c1df6e 100644 --- a/man/all_rows.Rd +++ b/man/all_rows.Rd @@ -7,6 +7,7 @@ \alias{min_rows} \alias{first_rows} \alias{last_rows} +\alias{sample_rows} \title{Subset data} \usage{ all_rows() @@ -20,6 +21,8 @@ min_rows(order_by, n, by = NULL, with_ties = TRUE, na_rm = FALSE) first_rows(n, by = NULL) last_rows(n, by = NULL) + +sample_rows(n, by = NULL) } \arguments{ \item{...}{<\code{\link[rlang:args_data_masking]{data-masking}}> Expressions that diff --git a/man/color_palettes.Rd b/man/color_palettes.Rd deleted file mode 100644 index 9d491e8c..00000000 --- a/man/color_palettes.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data.R -\docType{data} -\name{color_palettes} -\alias{color_palettes} -\title{Color palettes} -\format{ -A list of 18 color palettes. -} -\source{ -\code{tidyplots} package -} -\usage{ -color_palettes -} -\description{ -Color palettes -} -\examples{ -color_palettes -} -\keyword{datasets} diff --git a/man/colors_continuous_viridis.Rd b/man/colors_continuous_viridis.Rd new file mode 100644 index 00000000..9fdf63f4 --- /dev/null +++ b/man/colors_continuous_viridis.Rd @@ -0,0 +1,83 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/colors.R +\docType{data} +\name{colors_continuous_viridis} +\alias{colors_continuous_viridis} +\alias{colors_continuous_magma} +\alias{colors_continuous_inferno} +\alias{colors_continuous_turbo} +\alias{colors_continuous_blue_pink_yellow} +\title{Continuous color schemes} +\format{ +An object of class \code{tidycolor} (inherits from \code{character}) of length 265. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 265. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 265. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 265. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 11. +} +\usage{ +colors_continuous_viridis + +colors_continuous_magma + +colors_continuous_inferno + +colors_continuous_turbo + +colors_continuous_blue_pink_yellow +} +\description{ +Continuous color schemes +} +\details{ +Previews of build-in continuous color schemes\if{html}{\out{ + +}} +\if{html}{\out{ +
+

colors_continuous_viridis

+ A tidyplots color scheme with 265 colors, downsampled to 64 colors. +

+ +

+ c(
"#440154FF","#46075AFF","#470D60FF","#481466FF","#48196BFF","#481E6FFF","#482374FF","#482878FF","#472E7CFF","#46337FFF","#453881FF","#443C84FF","#424086FF","#404588FF","#3E4A89FF","#3C4F8AFF","#3A538BFF","#38578CFF","#375B8DFF","#34608DFF","#32648EFF","#31688EFF","#2F6C8EFF","#2D708EFF","#2B748EFF","#2A778EFF","#297B8EFF","#277F8EFF","#26828EFF","#24878EFF","#228B8DFF","#218F8DFF","#20928CFF","#1F958BFF","#1F998AFF","#1F9E89FF","#1FA187FF","#21A585FF","#22A983FF","#25AD82FF","#2BB07FFF","#2FB47CFF","#35B779FF","#3BBB75FF","#41BE71FF","#4BC16CFF","#53C569FF","#5BC864FF","#63CB5FFF","#6CCD5AFF","#77D153FF","#80D34DFF","#8AD647FF","#94D741FF","#9ED93BFF","#A8DB34FF","#B5DE2BFF","#C0DF25FF","#C9E11FFF","#D4E21AFF","#DEE318FF","#EBE51AFF","#F4E61EFF","#FDE725FF") +

+
+

colors_continuous_magma

+ A tidyplots color scheme with 265 colors, downsampled to 64 colors. +

+ +

+ c(
"#000004FF","#020109FF","#030311FF","#06051BFF","#0A0923FF","#0F0B2CFF","#130D34FF","#180F3DFF","#1E1149FF","#231252FF","#2A115BFF","#301164FF","#37106BFF","#3D0F71FF","#451077FF","#4C117AFF","#52137CFF","#58157EFF","#5E187FFF","#661A80FF","#6B1D81FF","#721F81FF","#782281FF","#7D2482FF","#852781FF","#8B2981FF","#922B81FF","#982D80FF","#9E2F7FFF","#A6317DFF","#AC347CFF","#B3367AFF","#B93779FF","#BF3A77FF","#C53C74FF","#CD4071FF","#D3436EFF","#D9466BFF","#DF4968FF","#E34E64FF","#E95562FF","#ED5A5FFF","#F1605DFF","#F4675CFF","#F66D5CFF","#F9775DFF","#FA7E5EFF","#FB8660FF","#FC8C63FF","#FD9467FF","#FE9D6CFF","#FEA470FF","#FEAB75FF","#FEB37BFF","#FEBA80FF","#FEC185FF","#FECA8DFF","#FED194FF","#FED89AFF","#FDDFA1FF","#FDE6A8FF","#FCEFB1FF","#FCF6B8FF","#FCFDBFFF") +

+
+

colors_continuous_inferno

+ A tidyplots color scheme with 265 colors, downsampled to 64 colors. +

+ +

+ c(
"#000004FF","#02010AFF","#040311FF","#08051CFF","#0B0725FF","#10092EFF","#150B37FF","#1B0C41FF","#230C4CFF","#290B54FF","#300A5CFF","#370962FF","#3D0965FF","#440A68FF","#4C0C6BFF","#520E6DFF","#59106EFF","#5E136EFF","#64156EFF","#6C186EFF","#721A6EFF","#781C6DFF","#7F1E6CFF","#85216BFF","#8D2369FF","#922568FF","#992766FF","#9F2A63FF","#A52C60FF","#AD305DFF","#B2325AFF","#B83556FF","#BE3853FF","#C33B4FFF","#C83F4BFF","#CF4446FF","#D44842FF","#D94D3DFF","#DE5239FF","#E15635FF","#E65D2FFF","#EA632AFF","#ED6925FF","#F06F20FF","#F3751BFF","#F67E14FF","#F7840FFF","#F98B0BFF","#FA9207FF","#FB9906FF","#FCA309FF","#FCA90EFF","#FCB115FF","#FBB91EFF","#FAC127FF","#F9C72FFF","#F7D13DFF","#F5D949FF","#F4E055FF","#F2E763FF","#F1EE73FF","#F3F587FF","#F6FA97FF","#FCFFA4FF") +

+
+

colors_continuous_turbo

+ A tidyplots color scheme with 265 colors, downsampled to 64 colors. +

+ +

+ c(
"#30123BFF","#351E57FF","#392971FF","#3E378EFF","#4141A4FF","#424CB6FF","#4456C8FF","#4661D6FF","#476DE6FF","#4777EFFF","#4681F7FF","#458BFCFF","#4295FFFF","#3D9EFEFF","#35ABF8FF","#2EB4F2FF","#27BEE9FF","#21C6E0FF","#1BCED7FF","#18D8C9FF","#18DEC0FF","#1AE4B6FF","#20EAACFF","#29EFA2FF","#37F492FF","#44F886FF","#53FA79FF","#62FC6CFF","#71FE5FFF","#83FF51FF","#91FF48FF","#9EFD3FFF","#A8FC39FF","#B2F936FF","#BCF534FF","#C8EF34FF","#D2E935FF","#DAE336FF","#E2DC38FF","#EAD439FF","#F1CA3AFF","#F6C33AFF","#FABA39FF","#FCB136FF","#FEA832FF","#FE9A2CFF","#FE8F28FF","#FC8323FF","#F9781EFF","#F66C19FF","#F15D13FF","#EC540FFF","#E74A0CFF","#E2420AFF","#DC3A07FF","#D43305FF","#CA2A04FF","#C12302FF","#B81D02FF","#AD1701FF","#A31301FF","#940C01FF","#880802FF","#7A0403FF") +

+
+

colors_continuous_blue_pink_yellow

+ A tidyplots color scheme with 11 colors. +

+ +

+ c(
"#00034D","#000F9F","#001CEF","#241EF5","#5823F6","#A033E0","#E85AB1","#F1907C","#F4AF63","#FCE552","#FFFB6D") +

+}} +} +\keyword{datasets} diff --git a/man/colors_discrete_metro_ui.Rd b/man/colors_discrete_metro_ui.Rd new file mode 100644 index 00000000..53ad7a1c --- /dev/null +++ b/man/colors_discrete_metro_ui.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/colors.R +\docType{data} +\name{colors_discrete_metro_ui} +\alias{colors_discrete_metro_ui} +\alias{colors_discrete_circle} +\title{Discrete color schemes} +\format{ +An object of class \code{tidycolor} (inherits from \code{character}) of length 5. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 6. +} +\usage{ +colors_discrete_metro_ui + +colors_discrete_circle +} +\description{ +Discrete color schemes +} +\details{ +Previews of build-in discrete color schemes\if{html}{\out{ + +}} +\if{html}{\out{ +
+

colors_discrete_metro_ui

+ A tidyplots color scheme with 5 colors. +

+ +

+ c(
"#4DACD6","#4FAE62","#F6C54D","#E37D46","#C02D45") +

+
+

colors_discrete_circle

+ A tidyplots color scheme with 6 colors. +

+ +

+ c(
"#939393","#644296","#F08533","#D1352C","#559E3F","#3B78B0") +

+}} +} +\keyword{datasets} diff --git a/man/colors_diverging_blue2brown.Rd b/man/colors_diverging_blue2brown.Rd new file mode 100644 index 00000000..18ff8534 --- /dev/null +++ b/man/colors_diverging_blue2brown.Rd @@ -0,0 +1,70 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/colors.R +\docType{data} +\name{colors_diverging_blue2brown} +\alias{colors_diverging_blue2brown} +\alias{colors_diverging_blue2red} +\alias{colors_diverging_RdBu} +\alias{colors_diverging_RdYlBu} +\title{Diverging color schemes} +\format{ +An object of class \code{tidycolor} (inherits from \code{character}) of length 17. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 3. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 11. + +An object of class \code{tidycolor} (inherits from \code{character}) of length 9. +} +\usage{ +colors_diverging_blue2brown + +colors_diverging_blue2red + +colors_diverging_RdBu + +colors_diverging_RdYlBu +} +\description{ +Diverging color schemes +} +\details{ +Previews of build-in diverging color schemes\if{html}{\out{ + +}} +\if{html}{\out{ +
+

colors_diverging_blue2brown

+ A tidyplots color scheme with 17 colors. +

+ +

+ c(
"#1961A5","#2671B5","#2D80BF","#268CC9","#119DD8","#00B2EB","#66C5EF","#C4E5F8","#FEFCF6","#FDEEB8","#FCDD67","#F6C445","#E78B43","#DD5642","#DB3E34","#CA3632","#B3322E") +

+
+

colors_diverging_blue2red

+ A tidyplots color scheme with 3 colors. +

+ +

+ c(
"#0000FF","#FFFFFF","#FF0000") +

+
+

colors_diverging_RdBu

+ A tidyplots color scheme with 11 colors. +

+ +

+ c(
"#67001F","#B2182B","#D6604D","#F4A582","#FDDBC7","#F7F7F7","#D1E5F0","#92C5DE","#4393C3","#2166AC","#053061") +

+
+

colors_diverging_RdYlBu

+ A tidyplots color scheme with 9 colors. +

+ +

+ c(
"#D73027","#F46D43","#FDAE61","#FEE090","#FFFFBF","#E0F3F8","#ABD9E9","#74ADD1","#4575B4") +

+}} +} +\keyword{datasets} diff --git a/man/figures/README-unnamed-chunk-11-3.png b/man/figures/README-unnamed-chunk-11-3.png index 11e7653d..e8e18caf 100644 Binary files a/man/figures/README-unnamed-chunk-11-3.png and b/man/figures/README-unnamed-chunk-11-3.png differ diff --git a/man/new_color_scheme.Rd b/man/new_color_scheme.Rd new file mode 100644 index 00000000..b1c57911 --- /dev/null +++ b/man/new_color_scheme.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/colors.R +\name{new_color_scheme} +\alias{new_color_scheme} +\title{New color scheme} +\usage{ +new_color_scheme(x, name = "Untitled color scheme") +} +\arguments{ +\item{x}{bla} + +\item{name}{bla} +} +\description{ +New color scheme +} diff --git a/man/show_color_palettes.Rd b/man/show_color_palettes.Rd deleted file mode 100644 index 75f19cfe..00000000 --- a/man/show_color_palettes.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/colors.R -\name{show_color_palettes} -\alias{show_color_palettes} -\title{Show color palettes} -\usage{ -show_color_palettes(pals = color_palettes, show_labels = TRUE, ncol = 3, ...) -} -\arguments{ -\item{pals}{bla} - -\item{show_labels}{bla} - -\item{ncol}{bla} - -\item{...}{bla} -} -\description{ -Show color palettes -} diff --git a/man/show_plot.Rd b/man/view_plot.Rd similarity index 57% rename from man/show_plot.Rd rename to man/view_plot.Rd index e939f071..e5169e7d 100644 --- a/man/show_plot.Rd +++ b/man/view_plot.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/plot.R -\name{show_plot} -\alias{show_plot} -\title{Show plot on screen} +\name{view_plot} +\alias{view_plot} +\title{View plot on screen} \usage{ -show_plot(gg, data = all_rows(), title = ggplot2::waiver(), ...) +view_plot(gg, data = all_rows(), title = ggplot2::waiver(), ...) } \arguments{ \item{gg}{bla} @@ -16,5 +16,5 @@ show_plot(gg, data = all_rows(), title = ggplot2::waiver(), ...) \item{...}{bla} } \description{ -Show plot on screen +View plot on screen } diff --git a/vignettes/Colors.Rmd b/vignettes/Colors.Rmd new file mode 100644 index 00000000..e890790d --- /dev/null +++ b/vignettes/Colors.Rmd @@ -0,0 +1,124 @@ +--- +title: "Colors" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Colors} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +# knitr::opts_chunk$set( +# collapse = TRUE, +# comment = "#>" +# ) +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + results = FALSE, + message = FALSE, + warning = FALSE, + fig.align = "center", + fig.width = 5, + fig.height = 2.5, + dpi = 300 +) + +``` + +```{r setup} +library(tidyplots) +``` + +```{r} + +colors_continuous_viridis +colors_discrete_metro_ui +colors_discrete_circle +colors_continuous_viridis +colors_continuous_magma +colors_continuous_blue_pink_yellow + +new_color_scheme(c('#d73027','#f46d43','#fdae61','#fee090','#ffffbf','#e0f3f8','#abd9e9','#74add1','#4575b4')) +new_color_scheme(c("#8ecae6", "#219ebc", "#023047", "#ffb703", "#fb8500")) +new_color_scheme(c("#cdb4db", "#ffc8dd", "#ffafcc", "#bde0fe", "#a2d2ff")) +new_color_scheme(c("#ef476f", "#ffd166", "#06d6a0", "#118ab2", "#073b4c")) +new_color_scheme(c("#390099", "#9e0059", "#ff0054", "#ff5400", "#ffbd00")) +new_color_scheme(c("#233d4d", "#fe7f2d", "#fcca46", "#a1c181", "#619b8a")) +new_color_scheme(c("#006ba6", "#0496ff", "#ffbc42", "#d81159", "#8f2d56")) +new_color_scheme(c("#ffc857", "#e9724c", "#c5283d", "#481d24", "#255f85")) +new_color_scheme(c("#edae49", "#d1495b", "#00798c", "#30638e", "#003d5b")) +new_color_scheme(c("#9b5de5", "#f15bb5", "#fee440", "#00bbf9", "#00f5d4")) +new_color_scheme(c("#072ac8", "#1e96fc", "#a2d6f9", "#fcf300", "#ffc600")) +new_color_scheme(c("#003f5c","#2f4b7c","#665191","#a05195","#d45087","#f95d6a","#ff7c43","#ffa600")) +new_color_scheme(c("#E64B35","#4DBBD5","#00A087","#3C5488","#F39B7F","#8491B4", + "#91D1C2","#DC0000","#7E6148","#B09C85")) + + +colors_discrete_metro_ui[2] +colors_continuous_blue_pink_yellow[4:8] +c(colors_continuous_blue_pink_yellow, colors_discrete_metro_ui) + + +# preview_colors_in_viewer(list( +# colors_continuous_viridis, +# colors_continuous_magma, +# colors_continuous_turbo, +# colors_continuous_inferno)) + +p1 <- + animals %>% + tidyplot(family, color = family) %>% + add_count_bar() + +p2 <- + animals %>% + tidyplot(animal, color = animal) %>% + add_count_bar() + +p3 <- + animals %>% + dplyr::filter(family != "Reptile") %>% + tidyplot(family, color = family) %>% + add_count_bar() + +p1 +# default +p1 %>% adjust_colors() +p1 %>% adjust_colors(colors_discrete_metro_ui) + +# correct number +p1 %>% adjust_colors(colors_discrete_metro_ui) + +# too many +p1 %>% adjust_colors(colors_continuous_blue_pink_yellow) +p1 %>% adjust_colors(colors_continuous_turbo) + +# too few +p1 %>% adjust_colors(colors_discrete_metro_ui[1:3]) + +p2 +# default +p2 %>% adjust_colors() +p2 %>% adjust_colors(colors_discrete_metro_ui) + +# too few +p2 %>% adjust_colors(colors_continuous_blue_pink_yellow) + +p3 +# default +p3 %>% adjust_colors() +p3 %>% adjust_colors(colors_discrete_metro_ui) + +# too many +p3 %>% adjust_colors(colors_continuous_viridis) + +``` + +```{r eval=FALSE} +colors_continuous_inferno +``` + +```{r results='asis', echo=FALSE} +print(colors_continuous_inferno, return_html = TRUE) +``` diff --git a/vignettes/Customized-defaults.Rmd b/vignettes/Customized-defaults.Rmd new file mode 100644 index 00000000..690168d6 --- /dev/null +++ b/vignettes/Customized-defaults.Rmd @@ -0,0 +1,55 @@ +--- +title: "Consistent style across plots" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Consistent-style} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +# knitr::opts_chunk$set( +# collapse = TRUE, +# comment = "#>" +# ) +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + results = FALSE, + message = FALSE, + warning = FALSE, + fig.align = "center", + fig.width = 5, + fig.height = 2.5, + dpi = 300 +) +``` + +```{r setup} +library(tidyplots) +``` + +```{r} + +my_style <- + . %>% + adjust_colors(colors_continuous_blue_pink_yellow) %>% + adjust_font(family = "mono", face = "bold") %>% + remove_x_axis_ticks() %>% + remove_y_axis_ticks() + +study %>% + tidyplot(group, score, color = treatment) %>% + add_mean_bar(alpha = 0.3) %>% + add_error() %>% + add_data_points_beeswarm() %>% + my_style() + +energy_week %>% + tidyplot(date, power, color = energy_source) %>% + add_areastack_relative() %>% + my_style() + +``` + + diff --git a/vignettes/tidyplots.Rmd b/vignettes/tidyplots.Rmd index 7f9510da..758038e2 100644 --- a/vignettes/tidyplots.Rmd +++ b/vignettes/tidyplots.Rmd @@ -25,7 +25,7 @@ knitr::opts_chunk$set( - `tidyplots` aims at delivering ready-to-publish plots, eliminating cumbersome formatting tasks. -- `tidyplots` uses the pipe `%>%` (instead of `+` like `ggplot2`) to build up plots. This way you can seamlessly pipe **into** and **out of** your plot. For example, coming from fram a data wrangeling pipeline you can generate a plot and directly pipe it into `save_plot()`. You can also call `save_plot()` or `show_plot()` in the middle of your pipeline to output intermediate results. +- `tidyplots` uses the pipe `%>%` (instead of `+` like `ggplot2`) to build up plots. This way you can seamlessly pipe **into** and **out of** your plot. For example, coming from fram a data wrangeling pipeline you can generate a plot and directly pipe it into `save_plot()`. You can also call `save_plot()` or `view_plot()` in the middle of your pipeline to output intermediate results. - `tidyplots` tries to reduce the complexity of `ggplot2` by choosing sensible defaults. However, you take more detailed control by via the `...` argument in each function. And if you need to add plain `ggplot2` code, you can do this using `add()` function, which will preserve the `tidyplots` pipeline. @@ -103,7 +103,11 @@ animals %>% add_mean_bar(alpha = 0.3) %>% add_error() %>% add_data_points() %>% - sort_x_axis_labels(size) + sort_x_axis_labels(size) %>% + adjust_colors(c("#B0B1B3", + "#F18823", + "#E23130", + "#1D5D83")) study %>% tidyplot(treatment, score, color = treatment) %>% @@ -263,14 +267,14 @@ new_colors <- "D" = "#1D5D83") p2 %>% adjust_colors(new_colors) -p2 %>% adjust_colors(new_colors, as_palette = TRUE) +p2 %>% adjust_colors(new_colors) # add_heatmap() - gene_expression %>% - tidyplot(x = sample, y = external_gene_name, color = expression) %>% - add_heatmap(scale = "row") %>% - adjust_plot_size(height = 90) +gene_expression %>% + tidyplot(x = sample, y = external_gene_name, color = expression) %>% + add_heatmap(scale = "row") %>% + adjust_plot_size(height = 90) gene_expression %>% tidyplot(x = sample, y = external_gene_name, color = expression) %>%