Open manodeep opened 4 years ago
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.
I thought someone already ported the colourmaps to R
- maybe they might contribute?
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. :)
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:
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.
@calofost Thank you very much for the example. I will add it to the documentation as soon as possible.
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)
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)
The contribution from @calofost is very comprehensive, but sometimes folks just need a quick palette instead of all the possible options.
Would be good to have instructions on how
R
/IDL
/Matlab
/etc users might be able to use thecmasher
colourmaps. Similarly, for certain application, this might be a good idea as well. Perhaps in the README or in the online documentation.