Introduction

Multiple plots on a graph is often desired; in lattice, panel or sometime as inset layouts. SAS and R, both have functionalities for these display, but have you ever thought of combining SAS and R plots? Not by cut and paste but programmatically. This is absolutely possible and I shall go over such a technique with some examples in this post.

Method

Here is the schematic of implementation from SAS perspective. Essentially plots are stored as png files then display at a specified location on a page with the drawimage function from the Graph Template Language (GTL). You will need SAS IML [1] or SAS to R interface such as described by Wei [2] to run R codes within SAS.

*** f_SAS_R_comb1.SAS ***;

*** This section of codes calls R to produce R plots;    
*** These saved plots were then subsequently inserted into a SAS plot; 
%callR(

library(lattice);
library(ggplot2);
library(flextable);

png("plot1.png", width=3000, height=3000, res=500);
   ... R codes for Base R plot;
dev.off();

png("plot2.png", width=3000, height=3000, res=500);
   ... R codes for lattice plot;
dev.off();

png("plot3.png", width=3000, height=3000, res=500);
   ... R codes for ggplot2 plot;
dev.off();

etc..

)

*** Produce the graph with all the plots;

 ods pdf file = "outgraph.pdf" style=mystyle nogfootnote dpi=300; 
*ods rtf file = "outgraph.rtf" style=mystyle nogfootnote headery=500 footery=500 dpi=300; 

ods graphics / noborder height=6in width=9.5in outputfmt=png; 
 
proc template;
define statgraph plottemp;
begingraph;

 *layout lattice / rows=1 columns=1;
  layout lattice / rows=1 columns=2;

    layout overlay;
       layout gridded / width=300px height=300px halign=0.1 valign=0.1;
          drawimage "plot1.png" / border=false layer=front anchor=bottomleft x=40 y=10 height=70 width=70;
       endlayout;
     
       layout gridded / width=400px height=200px halign=0.1 valign=0.1;
          drawimage "plot2.png" / border=false layer=front anchor=bottomleft x=80 y=10 height=70 width=70;
       endlayout;
         * etc;
    endlayout;

  endlayout;

endgraph;
end;
run;
 
proc sgrender data=dummy template=plottemp; 
run;



Results
Let’s look at some examples.

Example 1
This example shows a Plot from SAS SGPLOT and an R plot, side by side. Here, the layout is 1 row and 2 columns. Note we can even use the drawtext function in GTL to annotate the R plot!

Fig 1

Example 2
Sometime we want to inset plot(s) to a plot. In this example we plot the SAS graph with GTL then insert 2 R png images.

Fig 2

Example 3
This graph demonstrates the random arrangement of multiple plots from different graphical systems on a page. These include SGPLOT, Base R, lattice, and ggplot2. Note this graph and the other two are not results of cut and paste but they were generated programmatically.

Fig 3

Further Research

In this post I have looked from the drawimage method perspective, but there is also ODS layout [3]. Of course one could also look from the R perspective. That is let R do the displaying of the png images in instead of SAS [4].

Conclusion

Both SAS and R have powerful graphical systems and when harnessing their functionalities together, static graphics are limited only to your imagination.


Reference

[1] Duong Tran(Dec 2020). Calling R from Windows SAS
https://rpubs.com/dtran01/blog4

[2] Xin Wei (2012). %PROC_R: A SAS Macro that Enables Native R Programming in the Base SAS Environment https://www.jstatsoft.org/article/view/v046c02

[3] Michael Allie (2019). Using ODS LAYOUT to Create ABSOLUTE-ly Stunning Patient Profiles https://www.lexjansen.com/phuse/2019/dv/DV03.pdf

[4] Claus O. Wilke (Dec 2020). Mixing different plotting frameworks
https://wilkelab.org/cowplot/articles/mixing_plot_frameworks.html

Contact

Email: