cytoscape / RCy3

New version of RCy3, redesigned and collaboratively maintained by Cytoscape developer community
MIT License
48 stars 20 forks source link

Use viridis color palette for continuous mapping? #210

Closed mattias-erhardsson closed 3 months ago

mattias-erhardsson commented 10 months ago

Hi!

If I have understood this correctly, it is extremely difficult to use viridis color palettes in RCy3 for continuous color mapping.

For example, I try to map node color to a column of log2 fold changes with setNodeColorMapping:

RCy3::setNodeColorMapping("log2FoldChange", 
                          table.column.values = NULL,
                          mapping.type = "c",
                          colors = viridis,
                          style.name = "default")

This gives me the error "Error: #440154FF is not a valid hexadecimal color, e.g. #FD39B8."

I assume it is the hexadecimal color code for opacity (last two "FF" in example) that RCy3 can't handle.

For discrete mapping you can just modify the hexadecimal codes first and then feed to RCy3, but I can't figure out a way to do this with continuous mapping. Is there any way to make viridis work with RCy3 as it currently is? And if not, could this be implemented?

AlexanderPico commented 8 months ago

Indeed, we only implemented the Brewer palettes. You can see the list of support palette names in the code here: https://github.com/cytoscape/RCy3/blob/devel/R/StyleMappings.R#L2088

But, I agree we should add Viridis. Probably won't get that in time for this Fall's bioconductor release (in 2 days), but here's a workaround in the meantime. In fact, you can list any colors you want (without alpha channel), but then you also need to specify the control points as table.column.values. So, to put in your own viridis colors do this:

my.colors<-sapply(viridis::viridis(3), substr, 1, 7) #strip alpha
setNodeColorMapping('Degree', table.column.values = c(-3,0,3), colors=my.colors)

Note: You'll need to choose your min, mid, and max table.column.values. You can do this arbitrarily (like I did above) or check your data values and pick based on max(abs(min), abs(max)).

Also note: If your log2FoldChange values span 0 (i.e., contain both positive and negative), then we'd typically not recommend viridis, which is better for only positive (or only negative) ranges.