[R] pairs plot

Jim Lemon jim at bitwrit.com.au
Thu Oct 10 23:40:10 CEST 2013


On 10/11/2013 02:01 AM, Witold E Wolski wrote:
> my data are matrix with 3 numeric columns.
>
> would like to have pairs plot
> with scatterplots in the upper
> with hist at the diag
> and with correlation at the lower.
>
> actually default pairs does almost what I want but looks semi awesome.
> Especially, i didn't find out how to remove the axes from the lower
> part where I do only want to display the numeric values correlations
> there and somehow axes don't fit.
>
> Hence I am looking at ggpairs from GGally
> and calling it without parameters looks almost perfect :
> but I cant find out how they got the Corr: in the upper, so I can't
> put it in the lower,
> and I do not know how to put the hist in the diag.
>
Hi Witold,
The example below is roughly what you want, I think. This could be 
wrapped up in a function similar to "pairs" and I might do this in 
future if I get the time.

wwdat<-data.frame(a=seq(1,5,length.out=20)+rnorm(20),
  b=seq(1,4,length.out=20)+rnorm(20),c=rnorm(20))
require(plotrix)
panes(matrix(1:9,nrow=3,byrow=TRUE),c(1.2,1,1.2),c(1.2,1,1.2))
# plot 1
par(mar=c(0,3,3,0))
hist(wwdat$a,xaxt="n",xlab="",ylab="",main="")
box()
# plot 2
par(mar=c(0,0,3,0))
plot(wwdat$a,wwdat$b,xaxt="n",yaxt="n",xlab="",ylab="")
axis(3)
# plot 3
par(mar=c(0,0,3,3))
plot(wwdat$a,wwdat$c,xaxt="n",yaxt="n",xlab="",ylab="")
axis(3)
axis(4)
# plot 4
par(mar=c(0,3,0,0))
plot(0,0,xlim=c(-1,1),ylim=c(-1,1),xaxt="n",yaxt="n",xlab="",ylab="",type="n")
text(0,0,round(cor(wwdat$a,wwdat$b),2),cex=2.5)
# plot 5
par(mar=c(0,0,0,0))
hist(wwdat$b,xaxt="n",yaxt="n",xlab="",ylab="",main="")
# plot 6
par(mar=c(0,0,0,3))
plot(wwdat$b,wwdat$c,xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
# plot 7
par(mar=c(3,3,0,0))
plot(0,0,xlim=c(-1,1),ylim=c(-1,1),xaxt="n",yaxt="n",xlab="",ylab="",type="n")
text(0,0,round(cor(wwdat$c,wwdat$a),2),cex=2.5)
# plot 8
par(mar=c(3,0,0,0))
plot(0,0,xlim=c(-1,1),ylim=c(-1,1),xaxt="n",yaxt="n",xlab="",ylab="",type="n")
text(0,0,round(cor(wwdat$c,wwdat$b),2),cex=2.5)
# plot 9
par(mar=c(3,0,0,3))
hist(wwdat$c,xaxt="n",yaxt="n",xlab="",ylab="",main="")
axis(4)
box()

Jim



More information about the R-help mailing list