2

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.

7

You can use colorspace for sample plot

library(colorspace)
demoplot(c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c"), type = "bar")
  • 2
    This requires colorspace version >= v1.4-0 released Jan 2019. It was not in earlier versions of the package. – MrFlick Apr 16 at 16:11
  • Right, I use package ‘colorspace’ version 1.4-1. Thank you MrFlick – Yuriy Barvinchenko Apr 17 at 7:06
5

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)
1

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

  • 2
    Those actually aren't the right colors. Those are the default ggplot color pallete colors. – MrFlick Apr 16 at 15:32
  • Thanks, Mr. Flick. I did not recognize this. – MarBlo Apr 16 at 15:36
0

You can use command barplot with color option like below:

b <- barplot(1:length(vect)+length(vect):1, col = vect, border = FALSE, horiz = FALSE)
text(b,b,labels = vect, pos = 1, cex = 1) ## Naming the colors

1:length(vect)+length(vect):1 will produce a graph of parallel bars of same length.

In case you want to change the direction the of the graph you can use horiz = TRUE, In that case you may want to use pos = 3 to position the label appropriately on the graph.

Here vect vector is defined as per question like below:

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

0

A light-weight option that requires only grid,

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.