# How to make a conditioning plot?

I'd like to make a conditioning plot just like coplot in R. Is there a pre-made solution or do I have to make a bunch of plots individually?

Edit: This is an example from r screenshots. It is a way of looking at multivariate data. Each individual graph represents the respective bar in the conditioning variable or factor. In this case you see ozone vs solar for different levels of wind and temp.

Edit2: The R-integration looks interesting. I just found a StackExchange question about returning the plot to an output cell in Mathematica, so that works. I just thought there would be a built in function as these plots seem pretty popular in statistics. In r-code I would just type coplot(a~b|c) to see a vs b for levels of c.

• Please link to definition of "conditioning plot" is. Also "what" are you plotting? Is it data, functions? - give the specifics. Commented Jul 27, 2013 at 16:09
• Please check out slide 5, bottom section from the notebook that Dillon Tracy presented. Mathematica for Data Science. It uses the iris flower data set to develop a scatterplot similar to the one above. It is fully coded within the notebook. wolfram.com/events/virtual-conference/spring-2013/… Commented Jul 27, 2013 at 18:29
• @PatoCriollo That is called a pairs plot in R. gettinggeneticsdone.blogspot.ca/2011/07/… Commented Jul 27, 2013 at 19:15
• @PatoCriollo Thanks for the interesting link. I think the iris example actually needs improvement, though: the frame labels are cut off because there isn't enough ImagePadding... another common Mathematica plot issue. Anyway, I also can't say I'm familiar with the R terminology for plots, so a definition would be appreciated.
– Jens
Commented Jul 27, 2013 at 19:21
• Here are coplots of the iris data in R rrubyperlundich.blogspot.ca/2011/06/r-conditional-plot.html Better formatting and colors are possible of course. Commented Jul 27, 2013 at 19:26

## 2 Answers

Using the airquality data set already included in R. Don't know R commands so I used an example of coplot from the web.

Needs["RLink"];
InstallR[];
mathematicaRPlotWrapper = RFunction["function(filename, plotfun){
pdf(filename)
plotfun()
dev.off()
}"];
Clear[getRPlot];
getRPlot[plotFun_RFunction] :=
With[{tempfile = FileNameJoin[{$TemporaryDirectory, "temp.pdf"}]}, If[FileExistsQ[tempfile], DeleteFile[tempfile]]; mathematicaRPlotWrapper[tempfile, plotFun]; If[! FileExistsQ[tempfile], Return[$Failed]];
Import[tempfile]];
First@getRPlot@
RFunction[
"function(){coplot(Ozone~Temp|Solar.R, data=airquality)}"]

• Thanks. I don't know what the 'First@' is needed for, I deleted it and everything works. I tried your code and adding color but it doesn't work in PDF files so I switched to png. I then got an error saying "First::normal: Nonatomic expression expected at position 1 in First" so I deleted it. To add color you must escape the quotes. I'll try to add your code with my changes in an answer. Commented Jul 27, 2013 at 22:46
• I accepted your answer but I'm still curious if there's a native solution. I guess it means writing your own function in Mathematica. Commented Jul 27, 2013 at 23:08
Needs["RLink"];
InstallR[];

mathematicaRPlotWrapper = RFunction["function(filename, plotfun){
png(filename, width=600,height=600)
plotfun()
dev.off()
}"];
Clear[getRPlot];
getRPlot[plotFun_RFunction] :=
With[{tempfile = FileNameJoin[{$TemporaryDirectory, "temp.png"}]}, If[FileExistsQ[tempfile], DeleteFile[tempfile]]; mathematicaRPlotWrapper[tempfile, plotFun]; If[! FileExistsQ[tempfile], Return[$Failed]];
Import[tempfile]];

getRPlot@RFunction[
"function(){coplot(Ozone~Solar.R|Temp*Wind,data=airquality,number=4, col=\"red\")}"]

getRPlot@RFunction[
"function(){pairs(iris[1:4],main = \"Iris Data (cm)\",
pch = 21, bg = c(\"red\", \"green3\",\"blue\")[unclass(iris$Species)]) par(xpd=TRUE) legend(0, 1, as.vector(unique(iris$Species)),
fill=c(\"red\",\"green3\", \"blue\"))}"]

I put blank lines where I divided the cells. This way you just run the last cell whenever you need an R-plot. Note you have to escape the quotes on "red"

Just to show how easy it is to make the previous image in R and why I'd like the functionality in Mathematica. Even that pairs scatterplot linked to earlier is just one line now.

https://i.sstatic.net/ausmq.png