diff --git a/DESCRIPTION b/DESCRIPTION index c154cbc..1310fdf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,4 +27,5 @@ Imports: data.table, tibble, stringr, - tidyselect + tidyselect, + ggplot2 (>= 3.2.0) diff --git a/NAMESPACE b/NAMESPACE index 58c5e1e..add7b29 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,12 +15,14 @@ export(quick_forestfire) export(quick_graph) export(recursive_graph_join) export(rm_giant_component) +export(show_palette) export(str_replace_sp) export(str_replace_us) export(tidy_colnames) export(u_pull) export(unique_na) export(vsample) +importFrom(ggplot2,aes) importFrom(magrittr,"%<>%") importFrom(magrittr,"%>%") importFrom(rlang,"!!") diff --git a/R/show_palette.R b/R/show_palette.R new file mode 100644 index 0000000..9467942 --- /dev/null +++ b/R/show_palette.R @@ -0,0 +1,86 @@ +#' Show a color palette in a matrix +#' +#' This function is very simillar to the \code{show_cols()} in 'scales'. The +#' main difference is that this function returns a true \code{ggplot2} object. +#' This allows for further customization. +#' +#' @param cols The colors to present in a matrix. If the values are named, these +#' will be used for the cell label. If not, then the color value itself is used +#' for the label. +#' @param num_rows The number of rows to use or if the matrix should be as +#' square as possible. +#' @param label_size The size passed to \code{ggplot2::geom_text()} for the size +#' of the label. Default is 5. +#' @param base_size The base font size passed to \code{theme_bw()}. The default +#' is to use \code{label_size}. +#' @param font_family The font to use for thee plot. Default is "Times". +#' @param ... Any other information to pass to \code{ggplot2::theme()}. +#' +#' @return A \code{ggplot2} object. +#' +#' @examples +#' pal <- c("#3fc5f0", "#42dee1", "#6decb9", "#eef5b2") +#' show_palette(pal) +#' +#' show_palette(pal, num_rows = 1) +#' +#' names(pal) <- paste("color", 1:4) +#' show_palette(pal) +#' +#' @importFrom magrittr %>% +#' @importFrom ggplot2 aes +#' +#' @export show_palette +show_palette <- function(cols, + num_rows = "square", + label_size = 5, + font_family = "Times", + base_size = label_size, + ...) { + + if (is.null(names(cols))) { + col_names <- cols + } else { + col_names <- names(cols) + } + + if (num_rows == "square") { + num_rows <- ceiling(sqrt(length(cols))) + } else if (!is.numeric(num_rows)) { + stop("The `num_rows` argument must be either 'smart' or a numeric value.") + } + num_cols <- ceiling(length(cols) / num_rows) + + col_assignments <- c() + for (j in seq(num_rows, 1)) { + col_assignments <- c(col_assignments, rep(j, num_cols)) + } + row_assignments <- rep(seq(1, num_cols), num_rows) + + tibble::tibble(col_names = col_names, + color_vals = cols, + x = row_assignments[1:length(cols)], + y = col_assignments[1:length(cols)]) %>% + ggplot2::ggplot(aes(x = x, y = y)) + + ggplot2::geom_tile(aes(fill = color_vals), + color = NA) + + ggplot2::geom_text(aes(label = col_names), + size = label_size, + family = font_family) + + ggplot2::scale_fill_identity() + + ggplot2::scale_x_discrete(expand = c(0, 0)) + + ggplot2::scale_y_discrete(expand = c(0, 0)) + + ggplot2::theme_bw( + base_family = font_family, + base_size = base_size + ) + + ggplot2::theme( + axis.text = ggplot2::element_blank(), + axis.title = ggplot2::element_blank(), + axis.ticks = ggplot2::element_blank(), + ... + ) +} + +# for "show_palette" +utils::globalVariables(c("x", "y", "color_vals"), add = TRUE) diff --git a/_pkgdown.yml b/_pkgdown.yml index 74d9bfb..9b4325f 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -41,6 +41,10 @@ reference: - '`str_replace_us`' - '`str_replace_sp`' +- title: styling + contents: + - '`show_palette`' + - title: for 'tidygraph' contents: - '`filter_component_size`' diff --git a/docs/LICENSE.html b/docs/LICENSE.html index 86d74d3..90c1640 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -123,7 +123,7 @@

GNU General Public License

-

Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>

+

Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>

Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

@@ -142,9 +142,9 @@

TERMS AND CONDITIONS

-
+

-0. Definitions

+0. Definitions

“This License” refers to version 3 of the GNU General Public License.

“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.

“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations.

@@ -154,9 +154,9 @@

To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.

An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.

-
+

-1. Source Code

+1. Source Code

The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.

A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.

The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.

@@ -164,28 +164,28 @@

The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.

The Corresponding Source for a work in source code form is that same work.

-
+

-2. Basic Permissions

+2. Basic Permissions

All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.

You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.

Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.

-