#' Palette preview
#'
#' Preview all color palettes on a discrete and continuous scale
#'
#' @param discrete Palettes can be previewed on a discrete (\code{TRUE}) or continuous (\code{FALSE}) scale.
#'
#' @return A ggplot object (returned and displayed)
#'
#' @examples
#' preview_palettes()
#' preview_palettes(discrete = FALSE)
#'
#' @export
#'
preview_palettes <- function(discrete = TRUE){
pal <- chrestos_palettes()
if (!discrete) pal <- purrr::map(pal, function(x) grDevices::colorRampPalette(x)(101))
d_plot <- pal %>%
tibble::enframe() %>%
tidyr::unnest(value) %>%
dplyr::rename(
palette = name,
color = value
) %>%
dplyr::mutate(color_help = paste0(color, palette)) %>%
dplyr::mutate(palette = forcats::fct_inorder(palette)) %>%
dplyr::mutate(color_help = forcats::fct_inorder(color_help)) %>%
dplyr::mutate(scale = "discrete") %>%
dplyr::group_by(palette) %>%
dplyr::mutate(plot = 1/dplyr::n()) %>%
dplyr::ungroup()
col_plot <- d_plot %>%
dplyr::select(color_help, color) %>%
dplyr::distinct() %>%
dplyr::pull(color) %>%
as.character()
d_plot %>%
ggplot(aes(x = palette, y = plot, fill = color_help)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = col_plot) +
scale_y_reverse() +
coord_flip() +
theme_minimal() +
theme(
legend.position = "none",
panel.grid = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.title.x = ggplot2::element_blank()
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.