10-29-2013 04:51 PM
Using proc gplot, I currently have scatter plots and simulated bar graphs on the same graph. The simulated bar graph has 2 groupings. However, the outputted graph gives the simulated bars laying on top of one another. Is there anyway I can get grouped simulated bar graphs that instead lay side by side?
10-29-2013 05:14 PM
For SAS 9.2, you will need to transpose your data so that the two group values are in their own columns. Then, just modify the code below, and you will get your bar-scatter overlay. Run this example to see if this is what you want.
define statgraph bar_scatter;
barchart x=age y=height / stat=mean discreteoffset=-0.2 barwidth=0.4 fillattrs=graphdata1 name="a";
barchart x=age y=weight / stat=mean discreteoffset=0.2 barwidth=0.4 fillattrs=graphdata2 name="b";
scatterplot x=age y=height / discreteoffset=-0.2 markerattrs=graphdata1;
scatterplot x=age y=weight / discreteoffset=0.2 markerattrs=graphdata2;
discretelegend "a" "b";
proc sgrender data=sashelp.class template=bar_scatter;
10-30-2013 08:29 AM
I transposed my data, but the two bars are still laying on top of one another instead of beside each other. Unfortunately I cannot run the example you gave due to the different computers I have available to me, but I noticed the example did not use proc gplot. Is it able to be done using proc gplot (since everything with my graph is the way I want except for the bars not being beside each other) either with the original data or transposed data?
10-30-2013 08:48 AM
It all depends on exactly how your data is set up...
Assuming it's set up something like this:
x=1; y1=5; y2=3; output;
x=2; y1=6; y2=3.5; output;
And you're plotting it something like this (using wide gplot 'needles' to simulate bars, and the ones with the exact same x-coordinate are overlapping since they're in the exact same location)...
symbol1 value=none interpol=needle width=15;
axis1 order=(0 to 3 by 1) minor=none;
proc gplot data=foo;
plot y1*x y2*x / overlay vzero haxis=axis1;
Then you could modify your data by outputting 2 observations for each 1 observation in the original data, and apply a slight offset to the x-coordinate for each bar (a little to the left for one, and a little to the right for the other). The code could be something like this (but as I mentioned before, it all depends on exactly how your data is set up)...
data foo_mod; set foo;
x_mod=x-.04; colorvar=1; y_mod=y1; output;
x_mod=x+.04; colorvar=2; y_mod=y2; output;
proc gplot data=foo_mod;
plot y_mod*x_mod=colorvar / vzero haxis=axis1;
Below are the graphs for 'before' and 'after' ...
10-30-2013 10:43 AM
I have been following your website and using your fantastic code for a while, and I enjoyed the every bit of it, Thank you very much.
I have an off topic question for you and other SAS/Graph experts: Is SAS GTL to replace SAS/Graph? Is this where SAS is heading to? GTL comes with Base, so users do not have to pay additional for GRAPH.
10-30-2013 12:00 PM
SAS/GRAPH is not being replaced. It continues to be supported and enhanced. GTL and SG procedures are new additions to the tools available in SAS for creating graphs.
10-30-2013 01:51 PM
I offset the x-variables, and it worked like a charm! The only issue I'm having now is for some reason, the x-axis tick marks are not even remotely close to where the actual bars are. My x-axis should have 12 tick marks. Instead, it is only showing 6 of them, so my bars look like they are being compressed in between tick marks 2 and 5. Is this due to offsetting the x-variables? Or could something else be wrong?
10-30-2013 01:56 PM
I would need to see your data, and whether or not you have formats assigned to the x values, and what (if any) axis statement you're using for your x-axis, etc.
For example, if your X values are date values (using date formats) and you're using default x-axis (rather than using an axis statement with hardcoded ticks via the order=), etc, etc. With numeric values, the x-axis will default to "round" tickmarks, and if the x-axis has dates then it will pick tickmarks at the beginning of year, month, etc if it can.
Sounds like you'll just need to override the default tickmark values ... but I'd need to know what kind of values/formats/etc you're working with in order to provide you with some sample code.