xhr489
xhr489

Reputation: 2309

Print some HEX colors to view them

I have the following colors:

c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")

I would like to make simple plot so I can see the colors, either with ggplot2 or base R.

Upvotes: 3

Views: 956

Answers (4)

user11370352
user11370352

Reputation: 41

A light-weight option that requires only grid,

grid::grid.raster(t(c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")), interp=FALSE)

Upvotes: 0

MarBlo
MarBlo

Reputation: 4524

There is maybe a more elegant way, but this shows what you want:

library(ggplot2)

df <- data.frame(
    x = c(1,2,3,4,5,6),
    y = rep(1,6),
    c = c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")
 )
ggplot(df, aes(x,y, col=c))+geom_point(size=8)

This was wrong and I know the answers from above are the better ones But I wanted to make the improvements needed to offer at least a correct answer - at least what the choice of colors is concerned:

df <- data.frame(
  x = c(1,2,3,4,5,6),
  y = rep(1,6),
  c = c("#bbb487", "#066e9f", "#e2dfcc",
        "#4e766d", "#dd8047", "#d8b25c")
 )

farbe <- c("#bbb487", "#066e9f", "#e2dfcc",
          "#4e766d", "#dd8047", "#d8b25c")

ggplot(df, aes(x,y, col=c))+geom_point(size=8)+
  theme_bw()+
  scale_color_manual(
    name="Colors",
    values = farbe,
    breaks = c("#bbb487", "#066e9f", "#e2dfcc",
               "#4e766d", "#dd8047", "#d8b25c"))

enter image description here

Upvotes: 1

MrFlick
MrFlick

Reputation: 206197

With ggplot2 you can do

library(ggplot2)
show_colors <- function(colors) { 
  ggplot(data.frame(id=seq_along(colors), color=colors)) + 
    geom_tile(aes(id, 1, fill=color)) + 
    scale_fill_identity()
}

colors <- c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")
show_colors(colors)

which returns

enter image description here

Or you could turn it sideways and print the color names

show_colors2 <- function(colors) { 
  ggplot(data.frame(id=seq_along(colors), color=colors)) + 
    geom_tile(aes(1, id, fill=color)) + 
    geom_text(aes(1, id, label=color)) + 
    scale_fill_identity()
}
show_colors2(colors)

enter image description here

This base graphics you could do

show_colors <- function(colors) {
  ncol <- length(colors)
  plot(0,0, ylim=c(0, 1), xlim=c(0, ncol), type="n")
  rect(0:(ncol-1), 0, 1:ncol, 1, col = colors)
}
show_colors(colors)

Upvotes: 5

Yuriy Barvinchenko
Yuriy Barvinchenko

Reputation: 1595

You can use colorspace for sample plot

library(colorspace)
demoplot(c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c"), type = "bar")

Upvotes: 9

Related Questions