[R] Attaching marginal summary plots to the main matrix plot

Marc Schwartz mschwartz at medanalytics.com
Mon Aug 12 15:41:11 CEST 2002

> Dear helplist, I am trying to visualise a datamatrix together with its
> marginal densities.
> Suppose dat <- matrix( rnorm(1000), nrow=25 )
> I can the main visual plot using image, levelplot etc. But how do I
> the plots for rows sums and column sums to the top/bottom and right
> side of the original plot ?
> The only way I can think of is by using par(mfrow=c(2,2)) command but
> result is not desirable. Is there are useful command to do this ? If
> succesful I am thinking of affixing the boxplots by row and columns as
> I have gone through the lattice help but am still confused. Thank you.

If I correctly understand what you are trying to do, the way to do this
would be to use par("fig"), which adjusts the size of the plot region
within the current device.

par(mfrow) and par(mfcol) result in the device being split up into
equally sized plot regions, which does not achieve what I think you

Below is some code as an example.  I am using barplot() for the row and
column sum plots in the example.  This may or may not be how you want
these plotted.

I will leave the fine tuning of the plot types, sizes, axis ranges,
labels, row/column alignments, etc. for you. This should give you a
conceptual feel for how to go about it.




#define matrix
dat <- matrix(rnorm(1000), nrow = 25)

# save the current parameters 
op <- par(no.readonly = TRUE) 

# open a new plot window 

# adjust the plot region size for each area
# the LL and UR corners of the default region are (0,0) and (1,1) 
# par("fig") is (x1, x2, y1, y2) 

# first set the region for the larger plot at the bottom 
# note that the dimensions will overlap a bit in each region
par(fig = c(0.0, 0.75, 0.0, 0.75)) 

# now draw the larger plot 

# do not overwrite the current plot 
par(new = TRUE) 

# now set the second region for the small plot at the top 
par(fig = c(0.0, 0.75, .65, 1.0)) 

# plot the colSums

# do not overwrite the current plot 
par(new = TRUE) 

# now set the third region for the small plot to the right 
par(fig = c(0.65, 1.0, 0.0, 0.75)) 

# plot the rowSums
barplot(rowSums(dat), horiz = TRUE)

# restore the original parameters 

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list