[R] How to correct this
Mulholland, Tom
Tom.Mulholland at dpi.wa.gov.au
Tue Nov 23 03:29:34 CET 2004
This raises the question of "best practice." My answer was predicated on the fact that Jin Li had been attempting to use grid.circle in the first place without success. I rashly made the assumption that there was already a move to try and use some of the more sophisticated techniques within R.
This is a good example of the comments in the "hidden costs" thread, where the pathways to learning R came under some scrutiny. It is also similar to the "[R] How to insert one element into a vector?" where it is noted that append can be used to insert the element. That is the function appears to be originally written for one purpose, but it is evident that it has a broader application that is not immediately recognizable from the function name. When you are new to R it can seem confusing that you use rect for rectangles but symbols for circles, or segments for lines and lines for not lines, but they really are lines.
I am not yet proficient enough to always know which is the best approach. That's even with defining best as quickest, most easily maintained or most readable etc etc.
Now to the point. I have formed a collection of graphics that I have prepared over the last two years which I use to remind myself of the little idiosyncrasies of the various techniques. These of course have evolved as I have. They mostly use data that I cannot make available. I thought it might be a good idea to produce reproducible code that shows the bewildering variety ways to skin the proverbial animal. That is to produce code that can create a PDF flipbook of plots. One of the first things that I do when I load a package, is to run the examples that produce graphical output. I tend to work backwards and understand processes better when I know what the final output looks like. I am mathematically challenged, but can often appreciate what is happening once I see the plot. Ideally the code would include all the bells and whistles. I say this because I have spent hours trying to figure out just exactly what something is supposed to do before finally figuring out that it was really much simpler than I had thought. The bells and whistles should also show how you sometimes have to use par outside of the function (or remember that the ... is there for a reason) to get the effect that you want. For example when I load the vcd package to do mosaicplots I think I have to use par(xpd = TRUE) to get my multi-line labels not to be clipped.
As an evolving beast I see this as a way of demonstrating the techniques that are generally regarded as being "best practice" in a comprehensive manner.
In short I am volunteering. What for? I am not quite sure, but it includes example plots using data that helps in clarifying how the plot should be used. The last point means that I am not capable of producing some plots (and the examples in some packages already do this well) as I have no idea what they mean even when I have plotted the example.
Tom Mulholland
-----Original Message-----
From: Paul Murrell [mailto:p.murrell at auckland.ac.nz]
Sent: Tuesday, 23 November 2004 3:05 AM
To: Mulholland, Tom
Cc: Jin.Li at csiro.au; r-help at stat.math.ethz.ch
Subject: Re: [R] How to correct this
Hi
Mulholland, Tom wrote:
> Taking note of the first post, this is what I assume you wish. Note Paul's caveat in the help file
>
> "If you resize the device, all bets are off!"
>
> require(gridBase)
> x<-seq(0,1,0.2)
> y<-x
> pred<-matrix(c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.7, 0.7, 0.7,
> 0.5, 0.5, 0.7, 0.9, 0.9, 0.7, 0.5, 0.5, 0.7, 0.9, 0.9, 0.7, 0.5, 0.5,
> 0.7, 0.7, 0.7, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 6, 6)
> image(x, y, pred, col = gray(20:100/100), asp='s', axes=F, xlab=" ",
> ylab="")
> points(0.5, 0.5, col = 5) # the centre of the image
In this case, using grid (or gridBase) is probably overkill. The
symbols() function should do what you want. For example, ...
symbols(rep(0.5, 4), rep(0.5, 4), circles=1:4, add=TRUE)
Paul
> vps <- baseViewports()
> pushViewport(vps$plot)
> grid.circle(x=0.5, y=0.5, r=0.1, draw=TRUE, gp=gpar(col=5))
> grid.circle(x=0.5, y=0.5, r=0.3, draw=TRUE, gp=gpar(col=5))
> grid.circle(x=0.5, y=0.5, r=0.5, draw=TRUE, gp=gpar(col=5))
>
>
>
> -----Original Message-----
> From: Jin.Li at csiro.au [mailto:Jin.Li at csiro.au]
> Sent: Monday, 22 November 2004 1:21 PM
> To: r-help at stat.math.ethz.ch
> Subject: RE: [R] How to correct this
>
>
> Hi there,
>
> I would like to add a few circles to the following image:
> x<-seq(0,1,0.2)
> y<-x
> pred<-matrix(c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.7, 0.7, 0.7,
> 0.5, 0.5, 0.7, 0.9, 0.9, 0.7, 0.5, 0.5, 0.7, 0.9, 0.9, 0.7, 0.5, 0.5,
> 0.7, 0.7, 0.7, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 6, 6)
> image(x, y, pred, col = gray(20:100/100), asp='s', axes=F, xlab=" ",
> ylab="")
> points(0.5, 0.5, col = 5) # the centre of the image
>
> The centre of these circles needs to be overlapped with the centre of
> the image. Any helps are greatly appreciated.
> Regards,
> Jin
>
>
> -----Original Message-----
> From: Mulholland, Tom [mailto:Tom.Mulholland at dpi.wa.gov.au]
> Sent: Monday, 22 November 2004 12:29 P
> To: Li, Jin (CSE, Atherton)
> Subject: RE: [R] How to correct this
>
> I think you need to create a complete set of code that can be replicated
> by anyone trying to help.
> I ran the three grid.circle commands on my current plot and it did what
> I expected it to do. It plotted three circles centred in the current
> viewport. See the jpeg.
>
> The last command using points makes me think that you need to understand
> about units and the setting up of viewports. I have not played around
> with this much but I think thr newsletter had an article which may be of
> use (although it uses old code I think the differences are minor)
>
> Ciao, Tom
>
> -----Original Message-----
> From: Jin.Li at csiro.au [mailto:Jin.Li at csiro.au]
> Sent: Monday, 22 November 2004 10:07 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] How to correct this
>
>
> Hi there,
>
>
>
> I tried to add a few circles on an existing figure using the following
> codes
>
>
>
> grid.circle(x=0.5, y=0.5, r=0.1, draw=TRUE, gp=gpar(col=5))
>
> grid.circle(x=0.5, y=0.5, r=0.3, draw=TRUE, gp=gpar(col=5))
>
> grid.circle(x=0.5, y=0.5, r=0.5, draw=TRUE, gp=gpar(col=5))
>
> points(0.5, 0.5, col = 5) # centre of the circle
>
>
>
> , but all circles moved away from the centre. Could we do any
> corrections to this? Thanks.
>
>
>
> Regards,
>
>
>
> Jin
>
> ==========================
>
> Jin Li, PhD
>
> Climate Impacts Modeller
>
> CSIRO Sustainable Ecosystems
>
> Atherton, QLD 4883
>
> Australia
>
> Ph: 61 7 4091 8802
>
> Email: jin.li at csiro.au <mailto:jin.li at csiro.au>
>
> ==========================
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
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/
More information about the R-help
mailing list