Changing specific observation color (value text) with sgplot - forest plot

1.2k Views Asked by At

I am currently writing an sgplot code for forest plot (SAS University edition). I have managed to get the correct graph as I want, however I can not change the color of a specific observation. This is my code

data my_data;
    input study $ year rr lcl ucl;
    datalines;
    mickey 2015 1.5 0.7 2.3
    minny 2010 1.2 1.0 1.4
    donald 2013 0.8 0.2 1.4
    daisy 2014 1.3 1.0 1.6
    goofy 2017 1.9 0.9 2.9
    pluto 2010 1.4 0.7 2.1
    ;
run;

proc sgplot data=my_data
            noautolegend nocycleattrs; 

    scatter y=study x=rr/ markerattrs=(symbol=squarefilled size=12 color=black);
    highlow high=ucl low=lcl y=study / type=line lineattrs=(color=black);

    yaxistable study year / labelattrs=(family=arial size=12pt weight=bold) position=left location=inside valueattrs=(family=arial size=10pt);
    yaxistable rr lcl ucl / labelattrs=(family=arial size=12pt weight=bold) position=right location=inside valueattrs=(family=arial size=10pt);

    xaxis offsetmin=0.1 offsetmax=1 min=0.5 max=3.0 display=(nolabel);
    yaxis offsetmin=0.1 offsetmax=0.1 display=none reverse;

    refline 1 / axis=x;

    styleattrs axisextent=data;  
 run;

What I am trying to achieve is changing observation number 3 (donald, 2013, 0.8 0.2 1.4) into a red color (the text when plotting, not only the marker attributes).

I have tried to check for different sgplot attributes, but I am not able to change this specific color of observation number 3 (as red, and other observations remain black) when plotting. I have also looked at template, but this does not help. How can I achieve this?

1

There are 1 best solutions below

1
On BEST ANSWER

Assuming there is some programming logic behind the decision, one approach is to create a dummy group variable. Here I assume the logic is rr < 1.0.

I remove add colorgroup and attrid to the yaxistable in question. You can change what colors are assigned to the groups (to restore black for the majority) fairly easily, either by using an attribute map as I do below (most correct) or a few other options including editing the default graph colors.

data my_data;
    length groupvar $20;
    input study $ year rr lcl ucl;
    if rr < 1.0 then groupvar='redgroup';
    else groupvar='blackgroup';
    datalines;
    mickey 2015 1.5 0.7 2.3
    minny 2010 1.2 1.0 1.4
    donald 2013 0.8 0.2 1.4
    daisy 2014 1.3 1.0 1.6
    goofy 2017 1.9 0.9 2.9
    pluto 2010 1.4 0.7 2.1
    ;
run;


data attrmap;
length value $20;
input value $ textcolor $;
retain id 'colorgroup';
datalines;
redgroup red
blackgroup black
;;;;
run;

proc sgplot data=my_data
            noautolegend nocycleattrs dattrmap=attrmap; 

    scatter y=study x=rr/ markerattrs=(symbol=squarefilled size=12 color=black) group=groupvar attrid=colorgroup;
    highlow high=ucl low=lcl y=study / type=line lineattrs=(color=black);

    yaxistable study year / labelattrs=(family=arial size=12pt weight=bold) position=left location=inside valueattrs=(family=arial size=10pt)  
                            attrid=colorgroup colorgroup=groupvar;
    yaxistable rr lcl ucl / labelattrs=(family=arial size=12pt weight=bold) position=right location=inside valueattrs=(family=arial size=10pt) ;

    xaxis offsetmin=0.1 offsetmax=1 min=0.5 max=3.0 display=(nolabel);
    yaxis offsetmin=0.1 offsetmax=0.1 display=none reverse;

    refline 1 / axis=x;

    styleattrs axisextent=data;  
 run;