[R] symbol size on a plot

Paul Murrell p.murrell at auckland.ac.nz
Wed Jul 2 00:36:57 CEST 2003


Hi


Philippe Hupé wrote:
 > Hi,
 >
 > I would like to get from a plot the size of the symbols plotted.
 > Imagine I have the following plot function :
 > plot(1:2,1:2, pch=15, cex=4)
 > I would like the get the values SIZE1 and SIZE2 so that if I plot the
 > following rectangle :
 > rect(1.5,1.5, 1.5+SIZE1, 1.5+SIZE2) then the size of this square is
 > exactely the same as the one of the symbols that have been plotted.
 >
 > Thanks for any idea.


This is a bit tricky.  The size of plotting symbols is loosely based on 
the current cex setting (i.e., the size of text), BUT there are some 
"magic multipliers" applied within the C code to determine the exact 
size so that they look "nice".

There are some standard calculations you can perform to get close to the 
symbol size, but you have to do some fiddling to get exact and it will 
depend on which symbol you are plotting.  Here's an example for the 
default pch=1 circles ...

	plot(1:10)
	size1 <- (par("cin")[2]/par("pin")[1])*
	  (par("usr")[2] - par("usr")[1]) * 0.5 * par("cex") * 0.375
	size2 <- (par("cin")[2]/par("pin")[2])*
	  (par("usr")[4] - par("usr")[3]) * 0.5 * par("cex") * 0.375
	rect(5 - size1, 5 - size2, 5 + size1, 5 + size2)

... the 0.375 multiplier is an example of a "magic multiplier".  I got 
it from the following bits of C code in R/src/main/graphics.c

	#define RADIUS	0.375

	#define CMAG	1.0				

	#define GSTR_0  Rf_dpptr(dd)->cra[1] * 0.5 *
		Rf_gpptr(dd)->ipr[0] * Rf_gpptr(dd)->cex

	case 1: /* S octahedron ( circle) */
	    xc = CMAG * RADIUS * GSTR_0;

Further inspection of the C code would give you similar minor fiddles 
for the other plotting symbols.

Paul
-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz




More information about the R-help mailing list