Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the pairs function combined with layout in R?

Tags:

r

layout

I am trying to add a simple legend to a customized pairs graph.

Here is the reproductible code (without my customized pairs function) :

layout(cbind(1,2),width=c(1,1))
layout.show(2)
pairs(USJudgeRatings)

Why is the pairs function "erasing" my layout information ?

like image 590
Chapo Avatar asked Feb 19 '13 02:02

Chapo


People also ask

How does the pairs function work in R?

The pairs function is provided in R Language by default and it produces a matrix of scatterplots. The pairs() function takes the data frame as an argument and returns a matrix of scatter plots between each pair of variables in the data frame. Parameter: df: determines the data frame used for plotting to scatter plot.

What is a pairs plot in R?

A pairs plot is a matrix of scatterplots that lets you understand the pairwise relationship between different variables in a dataset. Fortunately it's easy to create a pairs plot in R by using the pairs() function. This tutorial provides several examples of how to use this function in practice.


1 Answers

A warning contained in the help for layout is

These functions are totally incompatible with the other mechanisms for arranging plots on a device: par(mfrow), par(mfcol)

Unfortunately, pairs uses mfrow for arranging the plots.

Using the hints from Duncan Murdoch and Uwe Ligges on R help, you can set oma to a reasonable value to give you room for a legend on the side, eg

pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species",
      pch = 21, bg = c("red", "green3", "blue")[iris$Species],
      oma=c(4,4,6,12))
# allow plotting of the legend outside the figure region 
# (ie within the space left by making the margins big)
par(xpd=TRUE)
legend(0.85, 0.7, as.vector(unique(iris$Species)),  
       fill=c("red", "green3", "blue"))

enter image description here

like image 56
mnel Avatar answered Sep 28 '22 07:09

mnel