[R] A panel of contour plots through a iteration process

Duncan Murdoch murdoch.duncan at gmail.com
Fri Nov 9 12:59:36 CET 2012


On 12-11-09 6:06 AM, Loukia Spineli wrote:
> Hm, the problem is a little bit more complicated than I thought. Let me
> give you more details. My data set is a list of two-dimensonal arrays of
> same dimensions. Each array must be plotted as a contour plot. Since my
> list has length 11, I want to plot 11 contour.plots in a window of 4 rows
> and 3 columns. So, this is the "challenge". At least I provide you the code
> where I tried to plot the contour plots iteratively using 'par'  (if only I
> knew that 'par' cannot solve my problem).

par() can solve your problem.  Just use the contour() function, or 
plot(x,y,type="n") followed by .filled.contour().

The code below is incomplete (no data provided) so I can't edit it for 
you, but the only tricky thing is that .filled.contour() has no 
defaults:  you need to compute all five arguments.

Duncan Murdoch

>
> par(mfrow=c(4,3))
> for(i in 1:length(unique(t))){
> pvalue_MA[[i]]<-matrix(pvalue_MA[[i]],nrow=length(px),ncol=length(py),byrow=T)
> filled.contour(pvalue_MA[[i]], zlim = range(pvalue_MA[[i]],
> finite=TRUE),xlab="% of missing success in intervention",
> ylab="% of missing success in control", plot.axes={
> # Do the rows, las=2 for text perpendicular to the axis
> x <- (1:dim(pvalue_MA[[i]])[1] - 1) / (dim(pvalue_MA[[i]])[1] - 1);
> axis(side=1, at=seq(0,1,0.2), labels=rownames(pvalue_MA[[i]]), las=2);
> # Do the columns
> y <- (1:dim(pvalue_MA[[i]])[2] - 1) / (dim(pvalue_MA[[i]])[2] - 1);
> axis(side=2, at=seq(0,1,0.2), labels=colnames(pvalue_MA[[i]]), las=2);
> # Add a solid black grid
> grid(nx=(dim(pvalue_MA[[i]])[1]-1), ny=(dim(pvalue_MA[[i]])[2]-1),
> col="black", lty="solid");
> # Add the trial-specific success rates within the observed cases in each
> group
> library(calibrate)  # A package for the function 'textxy')
> arraynames[[i]]<-array(0,dim=c(1,tn[i],1))
> a[[i]]<-array(0,dim=c(1,tn[i],1))
> v[[i]]<-array(0,dim=c(1,tn[i],1))
> for(k in 1:tn[i]){
> arraynames[[i]][[k]]<-id[t==i][k]
> a[[i]][[k]]<-sx[t==i][k]/(sx[t==i][k]+fx[t==i][k])
> v[[i]][[k]]<-sy[t==i][k]/(sy[t==i][k]+fy[t==i][k])
> # Add diagonal line
> abline(0,1)
> points(a[[i]],v[[i]],col="blue",pch=19)
> textxy(a[[i]],v[[i]],arraynames[[i]],cx=1.0,dcol="blue")
> }},col=mypal,axes=T,frame.plot=T,main="Graphical display of imputations")
> }
> par(mfrow=c(1,1))
>
> On Thu, Nov 8, 2012 at 7:13 PM, Loukia Spineli <spineliloukia26 at gmail.com>wrote:
>
>> Thank you very much both of you!!
>>
>> Jose, I will try your suggestion (regardless its application on my
>> "issue") to see how it works!:) You never know, Maybe, I will need to apply
>> it sometime. As for the par and the filled.contour I know that the layout
>> parameter thatis  already incorporated in filled.contour function "makes
>> our life harder" and we cannot use the (so easy to apply) par parameter...
>>
>> Thierry, the graph is really amazing!! This is what I want! Let's if I can
>> apply your suggestion in my data. I am curious to see the results.
>>
>>
>> On Thu, Nov 8, 2012 at 6:24 PM, Jose Iparraguirre <
>> Jose.Iparraguirre at ageuk.org.uk> wrote:
>>
>>> Thierry's solution is such more elegant and succinct. Loukia, you'd
>>> better forget about the multiplot() function for this...
>>>
>>> José
>>>
>>>
>>> -----Original Message-----
>>> From: ONKELINX, Thierry [mailto:Thierry.ONKELINX at inbo.be]
>>> Sent: 08 November 2012 16:12
>>> To: Jose Iparraguirre; Loukia Spineli; r-help at r-project.org help
>>> Subject: RE: [R] A panel of contour plots through a iteration process
>>>
>>> I would rather use facet_wrap() instead of multiplot()
>>>
>>> Just combine all your data in one data.frame and make sure that you have
>>> a variable indication the iteration.
>>>
>>> library(reshape2)
>>> volcano3d <- melt(volcano)
>>> names(volcano3d) = c("x", "y", "z")
>>> volcano3d <- merge(volcano3d, data.frame(Iteration = 1:4))
>>> volcano3d$z <- rnorm(nrow(volcano3d), mean = volcano3d$z, sd = 2)
>>>
>>> library(ggplot2)
>>> ggplot(volcano3d, aes(x, y, z = z)) + stat_contour(aes(colour =
>>> ..level..)) + facet_wrap(~Iteration)
>>>
>>>
>>>
>>> ir. Thierry Onkelinx
>>> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
>>> and Forest
>>> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
>>> Kliniekstraat 25
>>> 1070 Anderlecht
>>> Belgium
>>> + 32 2 525 02 51
>>> + 32 54 43 61 85
>>> Thierry.Onkelinx at inbo.be
>>> www.inbo.be
>>>
>>> To call in the statistician after the experiment is done may be no more
>>> than asking him to perform a post-mortem examination: he may be able to say
>>> what the experiment died of.
>>> ~ Sir Ronald Aylmer Fisher
>>>
>>> The plural of anecdote is not data.
>>> ~ Roger Brinner
>>>
>>> The combination of some data and an aching desire for an answer does not
>>> ensure that a reasonable answer can be extracted from a given body of data.
>>> ~ John Tukey
>>>
>>>
>>> -----Oorspronkelijk bericht-----
>>> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>>> Namens Jose Iparraguirre
>>> Verzonden: donderdag 8 november 2012 16:52
>>> Aan: Loukia Spineli; r-help at r-project.org help
>>> Onderwerp: Re: [R] A panel of contour plots through a iteration process
>>>
>>> Hi Loukia,
>>>
>>> I think the problem stems from the fact that the filled.contour "uses the
>>> layout function and so is restricted to a full page display" as its
>>> documentation reads. And if you look at the documentation of the layout
>>> function, it says that it is "totally incompatible with the other
>>> mechanisms for arranging plots on a device: par(mfrow), par(mfcol)".
>>>
>>> However, you can use the ggplot2 package along with the function
>>> multiplot, which you can find here:
>>> http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/
>>>
>>> It works fine with contour plots.
>>>
>>> You need to create your 11 contour plots first.
>>> For example, to keep things simple, let's create 11 equal contour plots:
>>>
>>>> library(reshape2)
>>>> volcano3d = melt(volcano)
>>>> names(volcano3d) = c("x", "y", "z")
>>>> library(ggplot2)
>>>
>>>> p1 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p2 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p3= ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p4 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p5 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p6 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p7 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p8 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p9 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p10 = ggplot(volcano3d, aes(x, y, z = z))+
>>> +  stat_contour(aes(colour = ..level..))
>>>> p11 = ggplot(volcano3d, aes(x, y, z = z))+  stat_contour(aes(colour =
>>>> ..level..))
>>>
>>> and then run the multiplot function:
>>>
>>>> multiplot(p1, p2, p3, p4, p5,p6,p7,p8,p9,p10,p11, cols=2) # the plots
>>> will be displayed along two columns.
>>>
>>> With so many plots in one page, you'll need to work on the individual
>>> margins of each plot and the size of the legends, etc, though!
>>>
>>> Hope this helps,
>>>
>>> José
>>>
>>> José Iparraguirre
>>> Chief Economist
>>> Age UK
>>>
>>> T 020 303 31482
>>> E Jose.Iparraguirre at ageuk.org.uk
>>> Twitter @jose.iparraguirre at ageuk
>>>
>>>
>>> Tavis House, 1- 6 Tavistock Square
>>> London, WC1H 9NB
>>> www.ageuk.org.uk | ageukblog.org.uk | @ageukcampaigns
>>>
>>>
>>> For a copy of our new Economic Monitor and the full Chief Economist's
>>> report, visit the Age UK Knowledge Hub
>>> http://www.ageuk.org.uk/professional-resources-home/knowledge-hub-evidence-statistics/
>>>
>>>
>>> For evidence and statistics on the older population, visit the Age UK
>>> Knowledge Hub
>>> http://www.ageuk.org.uk/professional-resources-home/knowledge-hub-evidence-statistics/
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>>> On Behalf Of Loukia Spineli
>>> Sent: 08 November 2012 13:11
>>> To: r-help at r-project.org help
>>> Subject: [R] A panel of contour plots through a iteration process
>>>
>>> Dear all,
>>>
>>> as you can see from the code I want to create *a panel of 11 contour
>>> plots through a iteration process*. I found a thread that address the issue
>>> of plotting many contour.plots in the same device, but it does not address
>>> my problem!
>>> I emphasize that the 11 contour plots must be appeared in the same device
>>> through an iteration process and NOT individually!
>>>
>>> par(mfrow=c(4,3)) # I want them to appear like a 4*3 matrix for(i in
>>> 1:11){
>>> p[i]]<-matrix(p[[i]],nrow=l10ncol=length10,byrow=T)
>>> filled.contour(p[i]], etc,etc...)
>>> }
>>> par(mfrow=c(1,1))
>>>
>>> Any suggestions would be really appreciated! Thank you very much in
>>> advance!!
>>>
>>> All the best,
>>> Loukia
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> Wrap Up & Run 10k next March to raise vital funds for Age UK
>>>
>>> Six exciting new 10k races are taking place throughout the country and we
>>> want you to join in the fun! Whether you're a runner or not, these are
>>> events are for everyone ~ from walking groups to serious athletes. The Age
>>> UK Events Team will provide you with a training plan to suit your level and
>>> lots of tips to make this your first successful challenge of 2012. Beat the
>>> January blues and raise some vital funds to help us prevent avoidable
>>> deaths amongst older people this winter.
>>>
>>>
>>> Sign up now! www.ageuk.org.uk/10k
>>>
>>> Coming to; London Crystal Palace, Southport, Tatton Park, Cheshire
>>> Harewood House, Leeds,Coventry, Exeter
>>>
>>>
>>> Age UK Improving later life
>>> www.ageuk.org.uk
>>>
>>>
>>>
>>>
>>> -------------------------------
>>> Age UK is a registered charity and company limited by guarantee,
>>> (registered charity number 1128267, registered company number 6825798).
>>> Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA.
>>>
>>> For the purposes of promoting Age UK Insurance, Age UK is an Appointed
>>> Representative of Age UK Enterprises Limited, Age UK is an Introducer
>>> Appointed Representative of JLT Benefit Solutions Limited and Simplyhealth
>>> Access for the purposes of introducing potential annuity and health cash
>>> plans customers respectively.  Age UK Enterprises Limited, JLT Benefit
>>> Solutions Limited and Simplyhealth Access are all authorised and regulated
>>> by the Financial Services Authority.
>>> ------------------------------
>>>
>>> This email and any files transmitted with it are confidential and
>>> intended solely for the use of the individual or entity to whom they are
>>> addressed. If you receive a message in error, please advise the sender and
>>> delete immediately.
>>>
>>> Except where this email is sent in the usual course of our business, any
>>> opinions expressed in this email are those of the author and do not
>>> necessarily reflect the opinions of Age UK or its subsidiaries and
>>> associated companies. Age UK monitors all e-mail transmissions passing
>>> through its network and may block or modify mails which are deemed to be
>>> unsuitable.
>>>
>>> Age Concern England (charity number 261794) and Help the Aged (charity
>>> number 272786) and their trading and other associated companies merged on
>>> 1st April 2009.  Together they have formed the Age UK Group, dedicated to
>>> improving the lives of people in later life.  The three national Age
>>> Concerns in Scotland, Northern Ireland and Wales have also merged with Help
>>> the Aged in these nations to form three registered charities:
>>> Age Scotland, Age NI, Age Cymru.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>> * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
>>> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver
>>> weer en binden het INBO onder geen enkel beding, zolang dit bericht niet
>>> bevestigd is door een geldig ondertekend document.
>>> The views expressed in this message and any annex are purely those of the
>>> writer and may not be regarded as stating an official position of INBO, as
>>> long as the message is not confirmed by a duly signed document.
>>>
>>> Wrap Up & Run 10k next March to raise vital funds for Age UK
>>>
>>> Six exciting new 10k races are taking place throughout the country and we
>>> want you to join in the fun! Whether you're a runner or not, these are
>>> events are for everyone ~ from walking groups to serious athletes. The
>>> Age UK Events Team will provide you with a training plan to suit your
>>> level and lots of tips to make this your first successful challenge of
>>> 2012. Beat the January blues and raise some vital funds to help us
>>> prevent avoidable deaths amongst older people this winter.
>>>
>>>
>>> Sign up now! www.ageuk.org.uk/10k
>>>
>>> Coming to; London Crystal Palace, Southport, Tatton Park, Cheshire
>>> Harewood House, Leeds,Coventry, Exeter
>>>
>>>
>>> Age UK Improving later life
>>> www.ageuk.org.uk
>>>
>>>
>>>
>>>
>>> -------------------------------
>>> Age UK is a registered charity and company limited by guarantee,
>>> (registered charity number 1128267, registered company number 6825798).
>>> Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA.
>>>
>>> For the purposes of promoting Age UK Insurance, Age UK is an Appointed
>>> Representative of Age UK Enterprises Limited, Age UK is an Introducer
>>> Appointed Representative of JLT Benefit Solutions Limited and
>>> Simplyhealth Access for the purposes of introducing potential annuity and
>>> health
>>> cash plans customers respectively.  Age UK Enterprises Limited, JLT
>>> Benefit Solutions Limited and Simplyhealth Access are all authorised and
>>> regulated by the Financial Services Authority.
>>> ------------------------------
>>>
>>> This email and any files transmitted with it are confidential and
>>> intended solely for the use of the individual or entity to whom they are
>>> addressed. If you receive a message in error, please advise the sender
>>> and delete immediately.
>>>
>>> Except where this email is sent in the usual course of our business, any
>>> opinions expressed in this email are those of the author and do not
>>> necessarily reflect the opinions of Age UK or its subsidiaries and
>>> associated companies. Age UK monitors all e-mail transmissions passing
>>> through its network and may block or modify mails which are deemed to be
>>> unsuitable.
>>>
>>> Age Concern England (charity number 261794) and Help the Aged (charity
>>> number 272786) and their trading and other associated companies merged
>>> on 1st April 2009.  Together they have formed the Age UK Group, dedicated
>>> to improving the lives of people in later life.  The three national
>>> Age Concerns in Scotland, Northern Ireland and Wales have also merged
>>> with Help the Aged in these nations to form three registered charities:
>>> Age Scotland, Age NI, Age Cymru.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>
> 	[[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list