Coder Social home page Coder Social logo

Comments (6)

calofost avatar calofost commented on June 15, 2024 3

I used the R package ‘RColorBrewer’ to load in the RGB colour scales. Installing RColorBrewer is very simple:

#If RColorBrewer is not already install, install it with:
install.packages('RColorBrewer')

Run the following code after you first download CMasher (or after every update) to create an RDS object that may be loaded into any R project. Be sure to edit the colormaps_path to the CMasher-master/cmasher/colormaps folder on your machine and the RDSfilepath to where you'd like to save the RDS object for future use.

#Load CMasher colours - must cite CMasher2020 - (i.e. van der Velden 2020)

#Loading RColorBrewer package.
library(RColorBrewer)

#Edit following paths:
colormaps_path='/path/to/CMasher-master/cmasher/colormaps'
RDSfilepath='/path/to/where/you/wanna/keep/the/RDS/object'

#Looking up the names of the scales and location of the _norm.txt files:
cmr_cmaps_files=list.files(path=colormaps_path, recursive=TRUE, all.files=TRUE, full.name=TRUE, pattern='_norm.txt')
cmr_cmaps_list=list.dirs(path=colormaps_path, full.names=FALSE)
cmr_cmaps_list=cmr_cmaps_list[2:length(cmr_cmaps_list)] #Removing "." from the list of directories.

#Loading all colour scales and adding them to a list object containing all scales called cmr_cmaps..
cmr_cmaps=list()
for (cmr_cmap in cmr_cmaps_list) {
  CM_RGB=read.table(file=cmr_cmaps_files[grep(pattern=cmr_cmap,cmr_cmaps_list)])
  colnames(CM_RGB)=c('R','G','B')
  assign(cmr_cmap, rgb(red=CM_RGB$R, green=CM_RGB$G, blue=CM_RGB$B))
  cmr_cmaps[[cmr_cmap]] = get(cmr_cmap)
}

#Create RDS file containing all CMasher colormaps into a single object:
saveRDS(cmr_cmaps,file=paste0(RDSfilepath, '/', 'cmr_cmaps.RDS'))

Then, when you're ready to load the CMasher scales again, just skip the above and run the following to get the scales back (be sure to update the RDSfilepath of course):

#Easy to load using the RDS object back into any R project using:
RDSfilepath='/path/to/where/you/wanna/keep/the/RDS/object'
cmr_cmaps=readRDS(file=paste0(RDSfilepath, '/', 'cmr_cmaps.RDS'))

There you go, the colour maps are all contained within the cmr_cmaps R object list. To access a specific colour map, just use the '$' operator : e.g. the rainforest is in cmr_cmaps$rainforest.

Below is an example use:

#Example use (volcano is an example dataset pre-loaded in every R installation):
par(mar=c(4,4,1,0.5))
layout(matrix(c(1,2),1, 2, byrow = TRUE), widths=c(3,1))
image(volcano, col = cmr_cmaps$rainforest, main = "rainforest")
par(mar=c(5,1,5,2.5))
image(y=seq(from=min(volcano),to=max(volcano),by=(diff(range(volcano)))/length(cmr_cmaps$rainforest)),z=t(1:length(cmr_cmaps$rainforest)), col=cmr_cmaps$rainforest, axes=FALSE, main="variable name", cex.main=.8)
axis(4,cex.axis=0.8,mgp=c(0,.5,0))

This is the output plot:
image

There are many other ways to make colour maps, but these functions are built-into R, so no need to install further packages apart from the RColorBrewer. These should run either in RStudio or on the R command-line. They can be incorporated within scripts easily.

from cmasher.

wkumler avatar wkumler commented on June 15, 2024 2

I'm a regular R user so I'll contribute to this thread a bit more too - found this package from your response on SO and struggled with the fancy RDS things above, so I wanted to demo a super simple way of using these colormaps as a one-off example rather than scraping all of them at once. R's able to read a table directly from the internet so we can just grab that (assuming we have an internet connection) and import it directly. rgb also isn't from the RColorbrewer package so we don't need to install/load that library to use it.

Here's some super-simple code that demos this process. It also allows you to just swap out the name to use a different one instead:

cmap_name <- "rainforest"
cmap_url <- paste0("https://raw.githubusercontent.com/1313e/CMasher/master/",
                   "cmasher/colormaps/", cmap_name, "/", cmap_name, "_norm.txt")
cmtable <- read.table(cmap_url)
cmscale <- rgb(cmtable[,1], cmtable[,2], cmtable[,3])
image(volcano, col=cmscale)

image

This also works with ggplot2 via the scale_*_gradientn function:

ggplot() + 
  geom_point(aes(x=1:100, y=1:100, color=1:100)) +
  scale_color_gradientn(colours = cmscale)

image

The contribution from @calofost is very comprehensive, but sometimes folks just need a quick palette instead of all the possible options.

from cmasher.

1313e avatar 1313e commented on June 15, 2024

That could be useful for others, yeah.
However, as I rarely use these languages, I don't know what the common way is of using colormaps in them.
Even if I would try to figure that out, it might not end up being the most optimal way of doing it.
So, flagging it with 'help wanted' for now to see if someone more experienced with these languages can help me with it.

from cmasher.

manodeep avatar manodeep commented on June 15, 2024

I thought someone already ported the colourmaps to R - maybe they might contribute?

from cmasher.

1313e avatar 1313e commented on June 15, 2024

I thought someone already ported the colourmaps to R - maybe they might contribute?

Oh yeah, @calofost did that at some point.
Would be great if they can help me out with this. :)

from cmasher.

1313e avatar 1313e commented on June 15, 2024

@calofost Thank you very much for the example.
I will add it to the documentation as soon as possible.

from cmasher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.