Is it possible to give a link in a sankey diagram a grading colour in R? - colors

I've made a sankey diagram to illustrate the change of a conclusion after an index event. The conclusion is noted as "Normal", "Borderline", or "Abnormal". I have changed the nodes of the diagram to reflect the conclusion by giving them either the color green, yellow or red. I would want to make the link from normal to borderline have gradient colour from green to yellow.
I managed to colour the different links between groups with a colour (e.g. from normal to borderline is yellow) by creating a vector called links$group and assigning a colour to the type of link (e.g. type_b is the link between normal to borderline and giving it a colour using the following function:
my_color <- 'd3.scaleOrdinal() .domain(["a", "b", "c", "type_a", "type_b", "type_c", "type_e"])
.range(["#9ACD32", "#FFD700", "#FF4500", "#CCCCCC", "#FFD700", "#FF4500", "#FF4500"])'
This is informative but a little hard on the eyes. I think i need to use the RColorBrewer"package. I made the following code:
type_b_color <- ramp <- colorRamp(c("#9ACD32", "#FFD700"))
and asigned it in the previous code as follows:
my_color <- 'd3.scaleOrdinal() .domain(["a", "b", "c", "type_a", "type_b", "type_c", "type_e"])
.range(["#9ACD32", "#FFD700", "#FF4500", "#CCCCCC", "type_b_color", "#FF4500",
This does however remove the color completely instead of letting it be gradient from the normal node (green) to the borderline node (yellow).

Related

How to manually set a diverging color range

I am using Altair for Python and my current code uses a redyellowblue color scheme which uses the middle color (yellow) accordingly to my domainMid parameter.
color=alt.Color('Spline_WW_Diff_Trend:Q', scale=alt.Scale(scheme='redyellowblue',reverse=True, domain=[-3.57,2.270], domainMid=0, clamp=True), legend=alt.Legend(title="Trend"))
Which gives me:
I need to replace the yellow color with the white color. I've switched scheme to range and tried different combinations of Scale parameters, but it doesn't respect my domainMid.
Example:
color=alt.Color('Spline_WW_Diff_Trend:Q', scale=alt.Scale(range=['#D4322C', 'white', '#4A74B4'],reverse=True, domain=[-3.57,2.270], domainMid=0, clamp=True), legend=alt.Legend(title="Trend"))
This gives me:
You can notice that the first column (which is all value 0) is showing reddish and not white (as it was supposed to be).
How can I have the same result as in the color scheme (picture one), but, with white instead of yellow?
Edit:
The same goes for.
range=['blue', 'lightblue', 'white', 'pink', 'red']
The mid color is light-blue, not white.
Edit 05/12/2022:
Just to clarify, I would like to achieve this same color scheme:
.
This heatmap was created in R, by using the RdYlBu color pallete (the one with 11 colors) and overwrite the middle (6th color) with white. Then, they increased the number of colors in the pallete to 99, to make the fade look more fluid. Does anyone has any idea on how to achieve that in Altair?
The easiest approach would be to set range='diverging', or to use one of the built-in diverging color schemes. For example:
import altair as alt
import pandas as pd
import numpy as np
df = pd.DataFrame({'x': np.arange(-10, 20)})
alt.Chart(df).mark_rect().encode(
x='x:O',
color=alt.Color('x:Q',
scale=alt.Scale(domainMid=0, scheme='redblue'))
)
If it's important to you that the center value is exactly white, you could use a condition to override the colormap for this value. For example:
alt.Chart(df).mark_rect().encode(
x='x:O',
color=alt.condition(
'datum.x == 0',
alt.value('white'),
alt.Color('x:Q', scale=alt.Scale(domainMid=0, scheme='redblue')))
)

What is the formula to generate two colour shade combos?

Attaching a sample image.
In which you can see a vivid pattern using two colours and the ranges in between.
I earlier thought of using the difference between the two colours and adding random values in that range to the smaller(hex) colour. But it gave me a very unpleasant palette.
First is what I need, Second is What I get.
When you generate colors, you should remember that they are built up from (R)ed (G)reen (B)lue, and sometimes Alpha-transparency.
Think of it: if you pick a random number between #000000 and #ffffff, you could get something like: #840523. Note that it's not gray, as you'd expect.
If you want it "random", you should pick random values for each channel.
So, in your example, do this:
Color1: #297EA6 split: #29 #7E #A6
Color2: #00101C split: #00 #10 #1C
Red : get a random value between #29 and #00 --> #20?
Green: get a random value between #7E and #10 --> #61?
Blue : get a random value between #A6 and #1C --> #8D?
New Color: #20 + #61 +#8D --> #20618D
If you want to keep the same tone, then you should consider calculating Hue/Saturation/Luminance values, and play with those. You might get more pleasing results.
In this answer I've explained how to do what you were trying to do, but make sure to read this too, because it goes deeper into interpolating colors: Interpolate from one color to another

Is there any way to give gradient color to normal color property in flutter?

I am using Bar Chart of fl_chart plugin. I want to give gradient color to bar graph. Below is the line where I want give color.
BarChartRodData(y: 12, color: Color(0xFF639ed1),width: 12)
But it is not possible to set gradient color to the ##color## property unless it is colors.
Is there any way to set gradient color to color property?? Please help me..
If you pass just one color, the solid color will be used, or if you pass more than one color, we use gradient mode to draw. then the gradientFrom, gradientTo and gradientColorStops
For an example ,
final List<Color> color = <Color>[];
color.add(Colors.blue[50]);
color.add(Colors.blue[100]);
color.add(Colors.blue);
final List<double> stops = <double>[];
stops.add(0.0);
stops.add(0.5);
stops.add(1.0);
Please refer https://pub.dev/documentation/fl_chart/latest/fl_chart/BarChartRodData-class.html for BarChartRodData class variables

d3.js color range and lighter color range

I would like to have two color ranges, the second one must consist off the same colors, but lighter.
rangeLength=10
color = d3.scale.linear().domain([1,rangeLength]).range(['red', 'blue']);
colorLigher= d3.scale.linear().domain([1,rangeLength]).range(['red'.lighter(10), 'blue'.lighter(10)]);
Obviously doesn't work, as 'red' is a string.
Thanks in advance
You can use the brighter() function (see the documentation):
length=10
color = d3.scale.linear().domain([1,length]).range(['red', 'blue']);
colorLighter= d3.scale.linear().domain([1,length])
.range([d3.rgb('red').brighter(), d3.rgb('blue').brighter()]);

Mathematica: how to have text in multiple colors?

I'd like to have a manipulate control, something like
{{wAB, 1, "AB"}, 0, 1, Appearance -> "Labeled"},
but would like the A and the B to be different colors, say, Red and Blue.
I can change the overall color with Style["AB",Red], but haven't been able to get the A and the B in different colors. Any help would be appreciated!
you mean like this?
Manipulate[
wAB,
{{wAB,1,Row[{Style["A", Red], Style["B", Blue]}]},0,1,Appearance->"Labeled"}
]
and if you prefer to define the decoration part separately (which can be useful for larger and more complicated controls) and reference it in controls later on (like declaring a variable, sort of, but it is a macro actually) and reuse it for different controls, then you can use With, like this
Manipulate[wAB,
Evaluate#With[
{myStyle = Row[{Style["A", Red], Style["B", Blue]}]},
{{wAB, 1, myStyle}, 0, 1, Appearance -> "Labeled"}
]
]
Although it is safer to do styling with Style you can actually color the characters of the string directly using the Format menu or keyboard shortcuts, and Mathematica will preserve it in the dynamic control:
If vertical position is x, you must print one letter on x, then count width (w) and print next letter with another color on the new position x+=w.

Resources