library(RColorBrewer)
library(tidyverse)
library(foreach)
library(reshape2)
# Matrix of colors ----
# specify
palettes <- c("BrBG", "PiYG", "PuOr", "RdBu", "RdGy")
n_colors <- 5
# take colors, name the column, then cbind
color_mat <- foreach(p = palettes, .combine = "cbind") %do% {
tibble(!!p := brewer.pal(n_colors, p))
}
# Plotting -----
# reshape for plotting:
color_df <- mutate(color_mat, order = 1:n())
long_color <- melt(color_df,
id.vars = "order",
variable.name = "palette",
value.name = "color")
# plot it, using scale_color_identity to interpret the hex as a color
ggplot(long_color, aes(x = palette, y = order, fill = color)) +
geom_tile() +
geom_text(aes(label = color)) +
scale_fill_identity()