The SAS 9.4 Maintenance 1 release is now shipping to users. This is great news for GTL and SG procedures users as this release includes some useful new features. Some of these are in direct response to your requests, and others are enhancements that we think you will come to like.
One new feature that falls in the second category is the new POLYGON plot. This is a "hybrid" plot that has the properties of a plot statement and Annotate. As a plot statement, it can be inserted anywhere in the sequence of plot statements as you are familiar with. So the graphics rendered by this plot will interleave between other plots. However, as the name of the plot suggests, you can draw simple (or complex) polygons defined by you. This will really open up the variety of graphs you can create. We will investigate many such plots in the next few blog article. If you like annotate, you will love this plot.
Simply put, you can draw polygons defined in either interval or categorical data space. The polygon is defined just like you would a SERIES plot. Each polygon has an ID, an X and a Y variable. X and Y can be numeric or discrete. The data set shown below defines two polygons with ids of 'X' and 'Y'. 'X' has a hole.
Here is the resulting graph. Note, the X variable is Name and is discrete and the Y variable is Y and is numeric. Data values that are discrete can have discrete offset for each obs, both in X and/or Y. This is like DiscreteOffset, but for each individual vertex. Note the lower vertex of the pink polygon is half way between B and C on the X axis. This can be a very useful feature. Click on the graph to see a higher resolution image.
Here is the SAS 9.4M1 code:
proc sgplot data=poly; polygon id=id x=name y=y / xoffset=offset group=id fill outline dataSkin=matte fillattrs=(transparency=0.5); keylegend / location=inside position=topleft; run;
As you probably realize, this plot statement can be used in many, many creative ways to build your graph. In this article, we will examine how you can build an Area Bar Chart, where the X and Y axis are both numeric, and each bar width is proportional to a response variable. Of course, we have to do a little bit of work to create the polygons as needed.
Here is some data on Revenues and Profits by Product. This is just made up data. Here is what the data set looks like:
From this data, we build a polygonal bar for each observation. The width of each bar is the Revenue for the observation, and the bar is placed to the right of the previous one on the X axis. The height of each bar is the Profit on the Y axis. See code in program to build this data set. Once done for all obs, the X dimension will represent the sum of all the revenue values.
Here is the graph created using this data and the Polygon plot using the SGPLOT procedure. Click on the graph for a higher resolution image.
SAS 9.4 M1 SGPLOT code:
proc sgplot data=areabar; title 'Revenue and Profit by Product'; polygon id=id x=x y=y / fill outline; yaxis offsetmin=0 grid label='Profit'; xaxis label='Revenue'; run;
Polygons can have labels, and the labels can be drawn in many different locations, positions and orientation. Here are some examples. Code for all cases is shown in the attached file.
SAS 9.4 M1 SGPLOT code:
proc sgplot data=areabar; title 'Revenue and Profit by Product'; polygon id=id x=x y=y / fill outline dataskin=gloss label=product labelpos=ymax rotatelabel=vertical; yaxis offsetmin=0 grid label='Profit'; xaxis label='Revenue'; run;
Here are some key features of this plot statement:
- A polygon is defined just like a SERIES plot, with a sequence of observations having the same ID.
- Each vertex can have numeric or discrete axis values.
- Each discrete value for a vertex can have an X and/or Y discrete offset.
- Polygons can have holes, indicated by missing X and Y values.
- Polygons can have label which is displayed at the bounding box center of polygon by default.
- Labels can be positioned inside the polygon, outside the polygon bounding box, or outside the axes.
- Labels can be horizontal or vertical.
- Each polygon can be rotated around its bounding box center.
- For rotated polygon, label can only be at center, but is also rotated.
Here is the same graph grouped by the Product Group. Note by default, the polygon labels use the contrast color attribute of the fill color. But, you can override this to use a fixed color. Legends are generated automatically as for other plots. In the graph below, we are still using rotated labels at the top of each polygon.
Here the label is a compound string made up of the product and its revenue. The label is shown at the top of the bar, using the default Split character.
As you can see, the POLYGON plot is a very versatile statement that will allow you to really customize your graph. Yes, you will need to generate the polygon vertices. The overlay axes recognize the data extents, and will union them with values from other plot statements. Legends are automatically generated. The plot will support both discrete and range attribute maps. The possibilities are endless.
Clearly, you can create maps using this statement. I will describe that use cases in a subsequent article. In the meantime, see if you can use the data from the MAPS library with this statement to create simple maps.
Full SAS 9.4 M1 Code: Polygon