Created
January 21, 2013 19:44
-
-
Save jstults/4588665 to your computer and use it in GitHub Desktop.
example of small multiple scatter plots and marginal densities in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ninputs = 3 # across the columns | |
noutputs = 2 # down the rows | |
iin = c(1,2,3) # indices for the inputs | |
jout = c(4,5) # indices for the ouputs | |
# example data frame: | |
npoints = 2**14 | |
data.set = data.frame(X1=rnorm(npoints), X2=rnorm(npoints), X3=rnorm(npoints)) | |
data.set = transform(data.set, X4=X1*X2+0.1*X3**2, X5=2*X2+X1+0.1*X3**2) | |
# the width/height are set by \showthe\columnwidth \textheight in latex | |
png("scatter_mat.png", height=6.500000276740003, width=8.215175176421752, units="in", pointsize=14, bg="transparent", res=600) | |
# bottom, left, top, right | |
par(mar=c(0,0,1,0), oma=c(0,0,0,0), fg="#00000022", col.axis="#00000080") # reduce margin, line-width, make axis grid and labels transparent | |
# add an extra row/column to provide labels | |
layout(matrix(seq((ninputs+1)*(noutputs+1)),noutputs+1,ninputs+1,byrow=TRUE), widths=c(1,rep(4,ninputs)), heights=c(1,rep(4,noutputs))) # adjust the first float in widths/heights to change the 'label' histogram size | |
dens = density(data.set$X1, bw="sj") | |
plot(dens$x, dens$y, axes=FALSE, type="n") # blank plot at 1,1 in the corner | |
# histograms across the top row: | |
for(i in iin){ | |
dens = density(data.set[,i], bw="sj") | |
plot(dens$x, dens$y, col="black", axes=FALSE,main=names(data.set)[i],cex=0.1) | |
} | |
for(j in jout){ | |
# row label: | |
par(mar=c(0,1,0,0), mgp=c(0,0,0)) # add room to the side for the ylabel | |
dens = density(data.set[,j], bw="sj") | |
plot(-dens$y, dens$x, col="black", axes=FALSE, ylab=names(data.set)[j], cex=0.1) | |
# scatter plots: | |
for(i in iin){ | |
par(mar=c(0,0,0,0)) # remove the margin | |
plot(data.set[,i], data.set[,j], pch=19, col="#00000022", cex=0.2, lwd=0.1) | |
} | |
} | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment