Color Bars

[This article was first published on Rstats – quantixed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here is a fun post about using colour palettes in R. It starts with a computer game…

The final level…

After a few years of sporadically playing Super Mario World 2 – Yoshi’s Island on the Retropie, I made it to the final level. In the background, as Bowser approached, I noticed that those coloured bars in the sky were reminiscent of the matplotlib palette, mpl-inferno.

mpl-inferno

So, I thought it would be fun to take the colours from the screenshot and try to use them in R for data visualization.

I loaded the screenshot into ImageJ, made a vertical line through the colours and extracted the red, green and blue values along the line as three csv files. You can access the code and files here if you want to play along.

# Working with a look-up table derived from RGB pixels
# In Data we have 3 csv files with pixel number and values for R, G, or B
rVal <- read.csv(file = "Data/RValues.csv", header = T)
gVal <- read.csv(file = "Data/GValues.csv", header = T)
bVal <- read.csv(file = "Data/BValues.csv", header = T)
df <- merge(rVal, gVal, by = "X")
df <- merge(df, bVal, by = "X")
names(df) <- c("X","R","G","B")
df$hex <- rgb(df$R, df$G, df$B, maxColorValue=255)
hexVal <- unique(df$hex)

At this point we have the hex values for the coloured bars. Let’s use these colours in a simple barplot.

# use the color palette
barplot(1:16, col = hexVal)

The first colour is black, which is out of sequence, so let’s get rid of it and reverse the order of the colours while we are at it. We can now use the discrete palette or an interpolated palette in an image plot (here using volcano as an example).

# delete the first value and reverse
yoshi <- rev(hexVal[2:16])
# use the discrete palette
image(volcano, col = yoshi)
# use an interpolated palette
filled.contour(volcano, color.palette = colorRampPalette(yoshi))

So there you have it, a palette for data visualization in R based on the final level of Yoshi’s Island.

Back to ImageJ

It would be great to use this palette as a look-up table back in ImageJ. So, can we export a *.lut file from R for use in ImageJ?

# make a look-up table for use in ImageJ
rgb.palette <- colorRampPalette(yoshi,space = "rgb")
hexpal <- rgb.palette(256)
rgbpal <- t(col2rgb(hexpal))
yoshiLUT <- data.frame(cbind(0:255,rgbpal))
names(yoshiLUT) <- c("Index","Red","Green","Blue")
write.table(yoshiLUT, file = "Output/Data/yoshiLUT.lut", sep = "\t", row.names = F)

The resulting file, yoshiLUT.lut could be loaded into ImageJ and applied to the Rat_Hippocampal_Neuron example image.

Rat_Hippocampal_Neuron (red channel, left) yoshiLUT version (right)

Conclusion

Making and using a color table from any starting image is possible, with a bit of data wrangling. Any suggestions for improvement to the code are welcome.

The post title comes from “Color Bars” by Elliott Smith from his Figure 8 LP.

To leave a comment for the author, please follow the link and comment on their blog: Rstats – quantixed.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)