Dani
Dani

Reputation: 53

Plot a vector of colors in R

I have this vector of colours in R:

"#88FF00" "#54FF00" "#FFFF00" "#13FF00" "#50FF00" "#87FF00" "#FDFB02" "#B8FF00" "#6DFF00" 
"#ECFF00" "#CBFF00" "#EDFF00" "#39FF00" "#B7FF00" "#75FF00" "#65FF00" "#C2FF00" "#25FF00" 
"#9AFF00" "#E6FF00" "#58FF00" "#E5CA1A" "#DEFF00" "#9AFF00" "#21FF00" "#35FF00" "#3AFF00" 
"#D0912F" "#FF0000" "#F7EF08" "#40FF00" "#DA7325" "#D8B027" "#DAFF00" "#55FF00" "#F8F007"
"#46FF00" "#69FF00" "#B8FF00" "#37FF00" "#32FF00" "#60FF00" "#7CFF00" "#00FF00" "#50FF00" 
"#F3E60C" "#DFBE20" "#7FFF00" "#F7EF08" "#99FF00" "#13FF00" "#DA7325" "#13FF00"

And I want to plot them in this order, something such as a heatmap. I had been toying with the image function but the colours are reordered and even modified by the image function.

The code that I tried is the following, when cols is the vector of colors mentioned above:

  image(1:nrow(expressionOrdered), 1:ncol(expressionOrdered), as.matrix(expressionOrdered), col= cols,xlab="",ylab="")

Upvotes: 4

Views: 6376

Answers (5)

PStaus
PStaus

Reputation: 50

you can use the scales::show_col function to simply visualize some color codes.

colors<- c("#88FF00", "#54FF00", "#FFFF00", "#13FF00", "#50FF00", "#87FF00",
           "#FDFB02", "#B8FF00", "#6DFF00", "#ECFF00", "#CBFF00", "#EDFF00",
           "#39FF00", "#B7FF00", "#75FF00", "#65FF00", "#C2FF00", "#25FF00",
           "#9AFF00", "#E6FF00", "#58FF00", "#E5CA1A", "#DEFF00", "#9AFF00",
           "#21FF00", "#35FF00", "#3AFF00", "#D0912F", "#FF0000", "#F7EF08",
           "#40FF00", "#DA7325", "#D8B027", "#DAFF00", "#55FF00", "#F8F007",
           "#46FF00", "#69FF00", "#B8FF00", "#37FF00", "#32FF00", "#60FF00",
           "#7CFF00", "#00FF00", "#50FF00", "#F3E60C", "#DFBE20", "#7FFF00",
           "#F7EF08", "#99FF00", "#13FF00", "#DA7325", "#13FF00")

scales::show_col(colors, ncol = 6, labels = FALSE)

enter image description here

Upvotes: 0

Dave Ross
Dave Ross

Reputation: 703

The contributed R package hues has a function named swatch that can generate the output shown here from a vector of colors.

x <- c("#88FF00", "#54FF00", "#FFFF00", "#13FF00", 
       "#50FF00", "#87FF00", "#FDFB02", "#B8FF00", "#6DFF00", 
       "#ECFF00", "#CBFF00", "#EDFF00", "#39FF00", "#B7FF00", 
       "#75FF00", "#65FF00", "#C2FF00", "#25FF00", "#9AFF00", 
       "#E6FF00", "#58FF00", "#E5CA1A", "#DEFF00", "#9AFF00", 
       "#21FF00", "#35FF00", "#3AFF00", "#D0912F", "#FF0000", 
       "#F7EF08", "#40FF00", "#DA7325", "#D8B027", "#DAFF00", 
       "#55FF00", "#F8F007", "#46FF00", "#69FF00", "#B8FF00", 
       "#37FF00", "#32FF00", "#60FF00", "#7CFF00", "#00FF00", 
       "#50FF00", "#F3E60C", "#DFBE20", "#7FFF00", "#F7EF08", 
       "#99FF00", "#13FF00", "#DA7325", "#13FF00")
hues::swatch(x)

color swatches

Upvotes: 1

Samuel Saari
Samuel Saari

Reputation: 1175

For simple cases, one could also this:

col_pal <- c("#2b8cbe","#de2d26", "#fc9272", "#fee0d2" , "#edf8b1" ,"#636363" )
plot(1:length(col_pal), 1:length(col_pal), col = col_pal, cex = 5, pch=19)

enter image description here

Upvotes: 0

bouncyball
bouncyball

Reputation: 10761

Here's another way:

plot(seq_len(length(COL)), rep_len(1, length(COL)),
     col = COL, pch = 16, cex = 3, xaxt = 'n', yaxt = 'n', xlab = '', ylab = '')

enter image description here

color vector from @G5W

COL = c("#88FF00", "#54FF00", "#FFFF00", "#13FF00", 
"#50FF00", "#87FF00", "#FDFB02", "#B8FF00", "#6DFF00", 
"#ECFF00", "#CBFF00", "#EDFF00", "#39FF00", "#B7FF00", 
"#75FF00", "#65FF00", "#C2FF00", "#25FF00", "#9AFF00", 
"#E6FF00", "#58FF00", "#E5CA1A", "#DEFF00", "#9AFF00", 
"#21FF00", "#35FF00", "#3AFF00", "#D0912F", "#FF0000", 
"#F7EF08", "#40FF00", "#DA7325", "#D8B027", "#DAFF00", 
"#55FF00", "#F8F007", "#46FF00", "#69FF00", "#B8FF00", 
"#37FF00", "#32FF00", "#60FF00", "#7CFF00", "#00FF00", 
"#50FF00", "#F3E60C", "#DFBE20", "#7FFF00", "#F7EF08", 
"#99FF00", "#13FF00", "#DA7325", "#13FF00")

Upvotes: 3

G5W
G5W

Reputation: 37641

How about just a bunch of rectangles?

COL = c("#88FF00", "#54FF00", "#FFFF00", "#13FF00", 
"#50FF00", "#87FF00", "#FDFB02", "#B8FF00", "#6DFF00", 
"#ECFF00", "#CBFF00", "#EDFF00", "#39FF00", "#B7FF00", 
"#75FF00", "#65FF00", "#C2FF00", "#25FF00", "#9AFF00", 
"#E6FF00", "#58FF00", "#E5CA1A", "#DEFF00", "#9AFF00", 
"#21FF00", "#35FF00", "#3AFF00", "#D0912F", "#FF0000", 
"#F7EF08", "#40FF00", "#DA7325", "#D8B027", "#DAFF00", 
"#55FF00", "#F8F007", "#46FF00", "#69FF00", "#B8FF00", 
"#37FF00", "#32FF00", "#60FF00", "#7CFF00", "#00FF00", 
"#50FF00", "#F3E60C", "#DFBE20", "#7FFF00", "#F7EF08", 
"#99FF00", "#13FF00", "#DA7325", "#13FF00")

plot(NULL, xlim=c(0,length(COL)), ylim=c(0,1), 
    xlab="", ylab="", xaxt="n", yaxt="n")
rect(0:(length(COL)-1), 0, 1:length(COL), 1, col=COL)

Rectangles

Upvotes: 6

Related Questions