# [R] Grid newbie: aligning & scaling viewports

Paul Murrell p.murrell at auckland.ac.nz
Sat Nov 20 02:18:40 CET 2010

```Hi

On 20/11/2010 2:10 p.m., Bryan Hanson wrote:
> Oh Knowledgeable Ones:
>
> I'm working on a project using grid graphics (for the first time).  A toy
> example is given below, run it simply with>foobar()  I am wondering why the
> pink dot and concentric circles are not centered on the 3-color axis system.
> Further, I feel like the concentric circles don't have the intended radius -
> if my math is right, the outmost circle should touch the ends of the colored
> axes.  My sense is that this has something to do with my mis-handling of the
> viewports, but I can't quite see it.  Any hints much appreciated!

It's your use of units.  Both the points (by default) and the circles
(explicitly) are using "native" coordinates and the scales on the
viewport are both -5 to 5, so (0.5, 0.5) on those scales is slightly
off-centre.  (0, 0) on those scales would be in the centre, as you have
specified for the segment start points.

Paul

> Bryan
> *************
> Bryan Hanson
> Professor of Chemistry&  Biochemistry
> DePauw University, Greencastle IN USA
>
> foobar<- function() {
>
> #    a couple of convenience functions before we begin:
>
>      p2cX<- function(r, theta) x<- r*cos(theta*2*pi/360)
>      p2cY<- function(r, theta) y<- r*sin(theta*2*pi/360)
>
>      vp<- viewport(x = 0.5, y = 0.5, width = 0.8, height = 0.8,
>          xscale = c(-5, 5), yscale = c(-5, 5))
>      grid.newpage()
>      pushViewport(vp)
>
>      grid.rect(gp = gpar(lty = "dashed", col = "gray")) # reference/guide
>
>      grid.points(x = 0.5, y = 0.5, pch = 20, gp = gpar(col = "pink"), vp =
> vp)
>
>      x0<- c(0,0,0)
>      y0<- c(0,0,0)
>      x1<- p2cX(c(4, 4, 4), c(0, 120, 240))
>      y1<- p2cY(c(4, 4, 4), c(0, 120, 240))
>
>      grid.segments(x0 = x0, y0 = y0, x1 = x1, y1 = y1,
>          gp = gpar(col = c("green", "blue", "red"), lwd = 2),
>          default.units = "native")
>
>      grid.circle(x = 0.5, y = 0.5, r = 1:4,
>              gp = gpar(col = "grey"), default.units = "native", vp = vp)
>
>      }
>
>> sessionInfo()
> R version 2.12.0 (2010-10-15)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
>   [1] splines   datasets  tools     grid      grDevices graphics  utils
> stats
>   [9] methods   base
>
> other attached packages:
>   [1] Hmisc_3.8-3        survival_2.35-8    gridExtra_0.7      GGally_0.2.2
>   [5] xtable_1.5-6       mvbutils_2.5.1     ggplot2_0.8.8      proto_0.3-8
>   [9] reshape_0.8.3      ChemoSpec_1.46     seriation_1.0-2
> colorspace_1.0-1
> [13] TSP_1.0-1          R.utils_1.5.3      R.oo_1.7.4
> R.methodsS3_1.2.1
> [17] rgl_0.92.794       lattice_0.19-13    mvoutlier_1.4      plyr_1.2.1
> [21] RColorBrewer_1.0-2 chemometrics_1.0   som_0.3-5
> robustbase_0.5-0-1
> [25] rpart_3.1-46       pls_2.1-0          pcaPP_1.8-3        mvtnorm_0.9-92
> [29] nnet_7.3-1         mclust_3.4.6       MASS_7.3-8         lars_0.9-7
> [33] gclus_1.3          cluster_1.13.1     e1071_1.5-24       class_7.3-2
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--
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
http://www.stat.auckland.ac.nz/~paul/

```