I have the following data set, and I want it to be plotted in ggplot2 as a color palette. I imagine it in bars stuck to each other with the color of each color in the dataset. Also, I calculated the distances of the colors so the bars are organized by proximity in terms of colors.
library(ggplot2)
library(tidyr)
library(dplyr)
# Your dataset
color_palette <- tibble::tribble( ~ Subfolder, ~ Color1, ~ Color2, ~ Color3, ~ Color4, ~ Color5,
"images/folder_1", "#1A1016", "#38282A", "#975D42", "#C8B7A2", "#E0D8C9",
"images/folder_2", "#2A2523", "#A97C5E", "#BDAC95", "#D6CCBC", "#F0EAE1")
# Convert the data to a long format
color_palette_long <- color_palette %>%
pivot_longer(cols = -Subfolder, names_to = "Distance", values_to = "Color") %>%
rowid_to_column("id")
color_distance <- function(color1, color2) {
col1 <- as.numeric(col2rgb(color1))
col2 <- as.numeric(col2rgb(color2))
sqrt(sum((col1 - col2)^2))
}
# Calculate color distances
color_palette_long <- color_palette_long %>%
rowwise() %>%
mutate(Distance = color_distance(Color, Color[1]))
# Create the ggplot
ggplot(color_palette_long, aes(x = Subfolder, y = id, fill = Color)) +
geom_bar(stat = "identity") +
scale_fill_identity() +
labs(x = NULL, y = NULL) +
theme_minimal()


One option if you are just looking for a nice way to represent a swatch of 5 colors would be a segmented pentagon: