Skip to content

Commit

Permalink
feat: show_palette() displays a color palette in a grid
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrcook committed Nov 10, 2019
1 parent 374de6f commit 5b1f05e
Show file tree
Hide file tree
Showing 13 changed files with 694 additions and 327 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Imports:
data.table,
tibble,
stringr,
tidyselect
tidyselect,
ggplot2 (>= 3.2.0)
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,"!!")
Expand Down
86 changes: 86 additions & 0 deletions R/show_palette.R
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ reference:
- '`str_replace_us`'
- '`str_replace_sp`'

- title: styling
contents:
- '`show_palette`'

- title: for 'tidygraph'
contents:
- '`filter_component_size`'
Expand Down
112 changes: 56 additions & 56 deletions docs/LICENSE.html

Large diffs are not rendered by default.

538 changes: 269 additions & 269 deletions docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pandoc: 2.3.1
pandoc: 2.7.3
pkgdown: 1.4.0
pkgdown_sha: ~
articles: []
Expand Down
15 changes: 15 additions & 0 deletions docs/reference/index.html

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

Binary file added docs/reference/show_palette-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/reference/show_palette-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/reference/show_palette-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
214 changes: 214 additions & 0 deletions docs/reference/show_palette.html

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

Loading

0 comments on commit 5b1f05e

Please sign in to comment.