Data visualization with SAS programming

Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Reply
Occasional Contributor
Posts: 12

Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Hi,

 

I'm trying to add a regression line to a GPLOT scatter plot, one plot per subject.  I have the slope and intercept of the regression line.  Is there a simple way to add the line?  I was able to get it going with PROC SGPLOT, but the output in the RTF file is much rougher.  It would be preferable just to enhance the  GPLOT.

 

Thanks in advance!

 

...dave

 

Here is my data (for one patient):

 

obs         subjid       session  thresh     slope      intercpt

35

36

101004

101004

1

2

-1.1750

-1.425

0.03274

0.03274

-1.57381

-1.57381

371010043-1.30.03274-1.57381
381010044-1.80.03274-1.57381
391010045-1.4250.03274-1.57381
401010046-1.4250.03274-1.57381
411010047-1.1750.03274-1.57381
421010048-1.30.03274-1.57381
431010049-1.30.03274-1.57381

 

Here is the existing GPLOT code (I'm leaving out the setup work for axes, legend, symbols, etc. to keep this post cleaner):

 

proc gplot data = lbplot; 
by trtn usubjid;
plot thresh*session=trtn /vaxis=axis2 haxis=axis1 frame legend=legend1;

 

 

Super User
Posts: 20,236

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

[ Edited ]
Posted in reply to davehalltwp

Can you use SGPLOT instead?

 

If so, it would be a single REG statement. 

 

EDIT: Sorry, I see you do have SGPLOT working. I would recommend sticking with that approach. I'm not sure what you meant by 'rougher' the output from SG procedures are much nicer than GPLOT in my experience. You can try changing the device, DPI or other settings to get better graphics, if necessary.

 

 

Occasional Contributor
Posts: 12

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Thanks for the reply, Reeza.  Yeah, I could use SGPLOT I suppose.  I don't know why the .rtf file it generated doesn't look as good.  Maybe all the setup the program did applied to the GPLOT but doesn't get applied to the SGPLOT?

 

What would be the REG line?  I tried something, I think maybe (PARMLINE?) and got a line, but it also insisted on printing a goofy box (giving the SLOPE and INTERCPT) on the plot that I couldn't suppress.

 

Thanks, I wish I knew more about plotting and graphing...

SAS Super FREQ
Posts: 1,184

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

Sometimes the default EMF Plus format output for RTF does not render very well.  However, you can use other image formats such as PNG.  Use OUTPUTFMT=PNG on the ODS Graphics statement.  Or, you can produce PDF output which also renders well.

Occasional Contributor
Posts: 12

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to Sanjay_SAS

Thanks, Sanjay.  I'll see if those are options.

Occasional Contributor
Posts: 12

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

http://support.sas.com/documentation/cdl/en/grstatproc/62603/HTML/default/viewer.htm#a003155517.htm

 

proc sgplot data=sashelp.class;
  reg x=height y=weight / CLM CLI;
run;

I haven't had a chance to test it, but I did a little research on Reeza's comment.  Does this REG statement produce a line based on the same SLOPE/INTERCEPT produced by PROC REG?  I know that's probably a really dumb question, but this seems too easy! 

Super User
Posts: 20,236

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

Pretty sure, it's easy enough to check though, create the one through your previous method and the REG statement and see how they look. There's also a way to get the formula embedded if you want to be 100% sure. 

Occasional Contributor
Posts: 12

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Wow, Reeza, displaying the formula was actually something that was requested.   I better look that up!  You all have been so helpful, thanks so much.

Super User
Posts: 20,236

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp
Super User
Posts: 20,236

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

None of the axis/legend statements that are used for the GRAPH procedures work for the SG procedures, they use their own syntax, which is easier to use in my opinion. 

 

I don't know much about graphing myself, but I know where to find examples and that's almost always my starting point. No one really writes code from scratch, do they? Smiley Wink

 

ods rtf file='/folders/myfolders/sample.rtf' style=meadow;

ods graphics / imagefmt=png;

proc sgplot data=sashelp.class;
  reg x=height y=weight / nolegfit ;
run;

ods rtf close;

http://documentation.sas.com/?docsetId=grstatproc&docsetTarget=n1gbe4z3i5l8hin1ajyvrwlcv00v.htm&docs...

 

Graph tip sheet

http://support.sas.com/rnd/app/ODSGraphics/TipSheet_ODSGraphics.pdf

 

ODS Graphics

http://support.sas.com/documentation/prod-p/grstat/9.4/en/PDF/odsbasicg.pdf?locale=en#nameddest=http...

SAS Super FREQ
Posts: 379

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

The REG statement in SGPLOT does not share code with PROC REG.  Rather, the REG statement and PBSPLINE statement use the same code, which was originally written for the PBSPLINE transformation in PROC TRANSREG.

 

Here is an example of putting inset formulas into a fit plot.

http://go.documentation.sas.com/?docsetId=statug&docsetTarget=statug_templt_sect017.htm&docsetVersio...

Super User
Posts: 11,578

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

If you want to overlay in Proc GPLOT you can't specify a different data set than the scatter. You would need to use a Plot2 thresh*session=subjid and require a LOT of symbol statements to do the regression plots. (Overlay won't work with y*x=z type plots)

 

Here is an example combining a scatter plot and regression in sgplot.

proc sgplot data=sashelp.class;
  scatter x=height y=weight;
  reg x=height y=weight / group=sex CLM CLI;
run;

If want to use your regression data with scatter you will need to combine the data with the scatter and then delve into the joys of the Graphic Template Language to use the LINEPARM plot type

 

SAS Super FREQ
Posts: 945

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

Try setting OUTPUTFMT=PNG and see if you like the RTF output better with SGPLOT.

 

ods graphics / outputfmt=png;

 

Hope this helps!

Dan

Occasional Contributor
Posts: 12

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

THanks to all.  My plot looks good!

 

 

SAS Employee
Posts: 982

Re: Syntax to Add a line of Regression to a simple GPLOT Scatter Plot

Posted in reply to davehalltwp

If you wanted to use gplot, you could annotate the line (and regression equation), and the annotate dataset could do that separately for each plot, based on the 'by' statement. (If you can share your data, and annotate dataset, I could create some code to do that for you.)

Ask a Question
Discussion stats
  • 14 replies
  • 214 views
  • 9 likes
  • 7 in conversation