Data visualization with SAS programming

Add a Frequency Table to a Plot

Accepted Solution Solved
Reply
Regular Contributor
Posts: 220
Accepted Solution

Add a Frequency Table to a Plot

I have created a waterfall plot using PROC GPLOT and the NEEDLE interpolation.  I want to display a frequency table on the plot (the table is created in the attached SAS code).  The table should be added to the bottom left corner of the plot and look like this:

PositiveNegative
Increased4022
Stable116
Decreased84

I know I can use an annotate dataset to add the table to the plot, but I have never used anno datasets and have no idea where to start.  I am having no luck trying to teach myself.  Can someone give me some help here?  I also only have SAS 9.1, so I cannot use the SG procedures, which I hear does this quite easily.

Attachment
Attachment

Accepted Solutions
Solution
‎02-14-2012 01:58 PM
SAS Employee
Posts: 967

Add a Frequency Table to a Plot

This should get you started with the basics (column headers could be added, and annotated move/draw lines or poly/polycont polygons or move/box could be used to draw boxes around the text - it's all a matter of geometry and working out the coordinates):

data table;
category='Increased'; positive=40; negative=22; output;
category='Stable'; positive=11; negative=6; output;
category='Decreased'; positive=8; negative=4; output;
run;

data anno_table; set table;
length text $20;
xsys='1'; ysys='1'; hsys='3'; when='a'; function='label';
y=95-_n_*5;
x=5; position='6'; text=trim(left(category)); output;
x=20; position='4'; text=trim(left(positive)); output;
x=25; position='4'; text=trim(left(negative)); output;
run;

proc gplot data=sashelp.class anno=anno_table;
plot height*weight;
run;

View solution in original post


All Replies
Esteemed Advisor
Posts: 7,293

Add a Frequency Table to a Plot

A lot of nice examples, including the code, can be found on Robert Allison's page ( http://robslink.com/SAS/Home.htm ).

Take a look, e.g., at group 17.  One at the bottom left side of the page includes a table on the bottom left part of the output.

SAS Employee
Posts: 967

Add a Frequency Table to a Plot

You can do things like this with annotate (using function='label'), but there's no easy cookie-cutter code that I can provide.  It all depends on the exact specifics of what you want...

Does the table always have 3 rows, or does it need to grow & shrink depending on how many 'bars' are in the waterfall chart?

Do you want the table in the same png file, but beside the graph, or inside the axes of the graph?  If inside the axes, is there guaranteed to always be white-space there?  (Beside the graph, you can guarantee white-space by using an angled blank title on the left or right of the graph).

Does the table have to have boxes drawn around it, or could it be just the words? (just the words is much simpler).

Here are some other examples with an annotated table, but none are exactly what you're wanting (just sort of randomly choosing a few - but none are really good simple/learning ones):

http://robslink.com/SAS/democd21/elecgage_info.htm

http://robslink.com/SAS/democd19/sfew_info.htm

http://robslink.com/SAS/democd33/subprime_info.htm

http://robslink.com/SAS/democd44/ex_29_info.htm

Unless you *reall/really* need to have the table sharing the same space as the graph, I would recommend putting the table under the graph (but on the same webpage) - then you can use a simple "proc print" (or other html table-creating procs).  These can really/truly be put together on the same webpage to look quite nice:

http://robslink.com/SAS/democd29/robslink_info.htm

Regular Contributor
Posts: 220

Add a Frequency Table to a Plot

Robert,

Thank  you for your response.

For these plots, all tables will have 3 rows.

I would prefer the table to be inside the axes, but if it is too much of a hassle, I may need to be more flexible.

I would also prefer the box around the data, but again, I can be flexible here.

I will keep looking at SAS papers and play around with the different annotate variables.  I found a paper by Art Carpenter that may help out a lot.

Thanks again!

Solution
‎02-14-2012 01:58 PM
SAS Employee
Posts: 967

Add a Frequency Table to a Plot

This should get you started with the basics (column headers could be added, and annotated move/draw lines or poly/polycont polygons or move/box could be used to draw boxes around the text - it's all a matter of geometry and working out the coordinates):

data table;
category='Increased'; positive=40; negative=22; output;
category='Stable'; positive=11; negative=6; output;
category='Decreased'; positive=8; negative=4; output;
run;

data anno_table; set table;
length text $20;
xsys='1'; ysys='1'; hsys='3'; when='a'; function='label';
y=95-_n_*5;
x=5; position='6'; text=trim(left(category)); output;
x=20; position='4'; text=trim(left(positive)); output;
x=25; position='4'; text=trim(left(negative)); output;
run;

proc gplot data=sashelp.class anno=anno_table;
plot height*weight;
run;

SAS Super FREQ
Posts: 8,719

Re: Add a Frequency Table to a Plot

In addition to Robert's excellent example, using GPLOT and ANNOTATE, the SGPLOT procedure with the NEEDLE statement can come close. If you are using 9.2, you'd have to stick with the INSET statement. If you are in 9.3, then the SG procedures do have annotation capability. Using your WATERFALL dataset, you should be able to see what this looks like using SAS 9.2 or higher.

cynthia

ods listing style=listing;
proc sgplot data=waterfall cycleattrs;
  title 'Best Percent Change in Tumor Size';
  where brpercent^=.;
  needle x=n y=brpercent / name='n1'
         group=posneg lineattrs=(thickness=3 pattern=1);
  yaxis grid label='Percent Change in Tumor Size';
  xaxis type=linear values=(1 to 91 by 1)  display=none;
  keylegend 'n1' / across=1 position=topright location=inside noborder;
  inset "                    Pos Neg"
        "Increased  111 112"
        "Stable         221 222"
        "Decreased 331 332" / position=bottomleft valuealign=left
                              border title="Inset Table 1";
  inset ("Increased"="111" "Stable"="222" "Decreased"="333")/
          position=bottom valuealign=left
          border title='Inset Table 2';

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 480 views
  • 6 likes
  • 4 in conversation