Assign specific color to legend based on specific values.

I am trying to create a scatter plot with 8 suplots (2x4) with gscatter. The first row plots get data from table A while the second from table B. The two datasets contain a column named "type" which I want to use as group/color in my scatter plots.
Everything works OK but...
While the same values appear in A.type and B.type, they don't get the same color. For example if A.type = 'man' gets green color, while if B.type = 'man' gets a red color which makes it frustrating in order to read on the same plot.
Is there a way to assign specific color to specific values?
Example code:
[...]
subplot(2,4,4)
gscatter(A.parameter3, A.parameter4, A.type);
subplot(1,4,5)
gscatter(B.parameter1, B.parameter2, B.type);
[...]

7 comentarios

dpb
dpb el 6 de Jul. de 2019
There's optional 'color' paramter to use.
"gscatter(x,y,group,clr,sym,siz) specifies the color, marker type, and size for each group. clr is either a character array of colors recognized by the plot function or a three-column matrix of color specifications. ... If you do not specify enough values for all groups,gscatter cycles through the specified values as needed."
Not clear just what you have in mind can be done by routine use of the grouping variables or not, but give that a shot first.
Without your data or at least a visual of what you have vis a vis what is wanted, hard to say exactly. You might be forced to build the plots piecemeal by judicious use of 'hold on' and multiple calls to have enough handles to the lines drawn; dunno about that w/o the full skinny.
Thank you for your reply. I am interested in assigning the following colours to the specific categories for all subplots:
  • Man - Blue
  • Woman - Red
  • Other - Green
  • Rather not say - Yellow
dpb
dpb el 6 de Jul. de 2019
Well, the above doc says specifically the color input variable clr specifies the color for each group. Did you try it?
Yes, but I want to to have the same color between the two datasets. While now, it assigns them randomly.
So in data, man appears with blue and in the second one appears with red. So I don't know how to set clr to take specific values (like the ones that are set above) and not randomly.
dpb
dpb el 7 de Jul. de 2019
Show how you set the color the same by group using the optional clr argument...the code you show doesn't use it.
But this is the answer I've been looking for! : )
dpb
dpb el 7 de Jul. de 2019
Editada: dpb el 7 de Jul. de 2019
"gscatter(x,y,group,clr,sym,siz) specifies the color, marker type, and size for each group. clr is either a character array of colors recognized by the plot function or a three-column matrix of color specifications. ... If you do not specify enough values for all groups,gscatter cycles through the specified values as needed."
Write
gscatter(A.parameter3, A.parameter4, A.type,yourcolorordervector);
as the doc instructs...and the example shows changing both the color and symbol for the two groups in the example dataset on the local R2017b doc or actually there's four-group example on current doc at https://www.mathworks.com/help/stats/gscatter.html?s_tid=doc_ta#mw_e8f95d9c-cb60-470e-844d-99fb39693e2f.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Jul. de 2019

Editada:

dpb
el 7 de Jul. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by