How to plot a ggplot2 a color palette data frame

73 Views Asked by At

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()
3

There are 3 best solutions below

0
Allan Cameron On

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

color_palette %>%
  mutate(rownumber = row_number()) %>%
  pivot_longer(-c(Subfolder, rownumber)) %>%
  mutate(boxnum = as.numeric(factor(name)) - 1, theta = pi * 72/180) %>%
  rowwise() %>%
  reframe(x = c(0, cos(boxnum * theta + pi/2), 
                cos((boxnum + 1) * theta + pi/2), 0),
          y = c(0, sin(boxnum * theta + pi/2), 
                sin((boxnum + 1) * theta + pi/2), 0) +
              rownumber * 3,
          textheight = 3 * rownumber,
          Subfolder = Subfolder, value = value,
          piece = cur_group_id()) %>%
  ggplot() +
  geom_polygon(aes(x = x, y = y, fill = value)) +
  geom_text(aes(x = -2, y = textheight, label = Subfolder), size = 8,
            hjust = 1) +
  scale_fill_identity() +
  coord_equal(clip = 'off') +
  theme_void()

enter image description here

0
jpsmith On

You can create a matrix of 1's then map the colors to it:

ones_rev_mat <- matrix(1, nrow = ncol(color_palette[-1]), 
                   ncol = nrow(color_palette))

barplot(ones_rev_mat, beside = TRUE, col = as.matrix(t(color_palette[,-1])))

enter image description here

0
MrFlick On

Changing your ggplot code slightly you can do

ggplot(color_palette_long, aes(x = Subfolder, y = 1, fill = Color)) +
  geom_col() +
  scale_fill_identity() +
  labs(x = NULL, y = NULL) +
  theme_minimal()

enter image description here

If you had a different number of colors per group, you could use geom_col(position="fill") to make sure all the columns stretched to the same size.