Plot multiple columns

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Plot multiple columns

Noah Silverman-3
I'm running a long MCMC chain that is generating samples for 22 variables.

I have each run of the chain as a row in a matrix.

So:  Chain[,1] is the column with all the samples for variable one.
Chain[,2] is the column with all the samples for variable 2, etc.

I'd like to fit all 22 on a single page to print a nice summary.  It is
OK if the graphs are small, I just need to show the overall shape and
convergence.

Using par(mfrow=(11,2)) gives me the error:  "figure margins too large"
when I try to draw a plot
I looked at the Lattice package, which seems very promising, but I can't
figure out how to have it plot each column in a separate box.

I need to do this same "one page" summary about 50 times, so it would be
a nightmare to make over 1,000 plots by hand.

Ideally, I'd create a loop for each of the 50 runs that would generate a
single page containing the 22 plots.

In pseudocode:

for( i in 1:50){
    par(mfrow(11,2))
    for(j in 1:22){
        plot(Chain[,j], type="l")
    }
}

BUT, this doesn't work.

Does anyone have any ideas about an easy way to do this?

Thanks!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

Ivan Calandra
Hi,

Try with:
par(mfrow=c(11,2))

It should work better!
Remember that mfrow is an argument of the function par(), not a function
itself.

One other tip: think about using pdf, ps, png, or SVG devices, I find it
easier and nicer.

HTH,
Ivan

Le 6/1/2010 10:02, Noah Silverman a écrit :

> I'm running a long MCMC chain that is generating samples for 22 variables.
>
> I have each run of the chain as a row in a matrix.
>
> So:  Chain[,1] is the column with all the samples for variable one.
> Chain[,2] is the column with all the samples for variable 2, etc.
>
> I'd like to fit all 22 on a single page to print a nice summary.  It is
> OK if the graphs are small, I just need to show the overall shape and
> convergence.
>
> Using par(mfrow=(11,2)) gives me the error:  "figure margins too large"
> when I try to draw a plot
> I looked at the Lattice package, which seems very promising, but I can't
> figure out how to have it plot each column in a separate box.
>
> I need to do this same "one page" summary about 50 times, so it would be
> a nightmare to make over 1,000 plots by hand.
>
> Ideally, I'd create a loop for each of the 50 runs that would generate a
> single page containing the 22 plots.
>
> In pseudocode:
>
> for( i in 1:50){
>      par(mfrow(11,2))
>      for(j in 1:22){
>          plot(Chain[,j], type="l")
>      }
> }
>
> BUT, this doesn't work.
>
> Does anyone have any ideas about an easy way to do this?
>
> Thanks!
>
> ______________________________________________
> [hidden email] 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.
>
>    

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

baptiste auguie-5
In reply to this post by Noah Silverman-3
Hi,

You could use melt from the reshape package to create a long format
data.frame. This is more easy to plot with lattice or ggplot2, and you
can then use facetting to arrange several plots on the same page. The
dummy example below produces 10 pages of output with 10 graphs per
page.

library(ggplot2)

dl <- replicate(10, as.data.frame(matrix(rnorm(1e3), ncol=10)), simplify=FALSE)
names(dl) <- paste("column", seq_along(dl), sep="")

# dummy list of data.frames
str(dl)

# a function to plot one data.frame
plotone <- function(d, ...)
  qplot(seq_along(value), value, data=melt(d)) +
  facet_wrap(~variable, scales="free")

# call the function for each data.frame
pl <- llply(dl, plotone)

# print to a file
pdf("test.pdf")
l_ply(pl, print)
dev.off()


HTH,

baptiste

On 1 June 2010 10:02, Noah Silverman <[hidden email]> wrote:

> I'm running a long MCMC chain that is generating samples for 22 variables.
>
> I have each run of the chain as a row in a matrix.
>
> So:  Chain[,1] is the column with all the samples for variable one.
> Chain[,2] is the column with all the samples for variable 2, etc.
>
> I'd like to fit all 22 on a single page to print a nice summary.  It is
> OK if the graphs are small, I just need to show the overall shape and
> convergence.
>
> Using par(mfrow=(11,2)) gives me the error:  "figure margins too large"
> when I try to draw a plot
> I looked at the Lattice package, which seems very promising, but I can't
> figure out how to have it plot each column in a separate box.
>
> I need to do this same "one page" summary about 50 times, so it would be
> a nightmare to make over 1,000 plots by hand.
>
> Ideally, I'd create a loop for each of the 50 runs that would generate a
> single page containing the 22 plots.
>
> In pseudocode:
>
> for( i in 1:50){
>    par(mfrow(11,2))
>    for(j in 1:22){
>        plot(Chain[,j], type="l")
>    }
> }
>
> BUT, this doesn't work.
>
> Does anyone have any ideas about an easy way to do this?
>
> Thanks!
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

Ben Bolker
In reply to this post by Noah Silverman-3
Noah Silverman <noah <at> smartmediacorp.com> writes:

>
> I'm running a long MCMC chain that is generating samples for 22 variables.
>
> I have each run of the chain as a row in a matrix.

> So:  Chain[,1] is the column with all the samples for variable one.
> Chain[,2] is the column with all the samples for variable 2, etc.

  The previous 2 paragraphs seemed contradictory until I realized
that in the first paragraph you are using "run" to mean what I would
usually call a "sample" ...

> I'd like to fit all 22 on a single page to print a nice summary.  It is
> OK if the graphs are small, I just need to show the overall shape and
> convergence.

  How about for example

> x <- matrix(runif(22000),ncol=22)
> library(coda)
> m <- as.mcmc(x)
> xyplot(m)
> xyplot(m,layout=c(4,6))

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

Noah Silverman-3
Hi,

I used the term "run", as each iteration of the Gibbs sampler produces
22 variables (coefficients for Beta in a regression model)


The example wont work


On 6/1/10 5:54 AM, Ben Bolker wrote:

> Noah Silverman <noah <at> smartmediacorp.com> writes:
>
>  
>> I'm running a long MCMC chain that is generating samples for 22 variables.
>>
>> I have each run of the chain as a row in a matrix.
>>    
>  
>> So:  Chain[,1] is the column with all the samples for variable one.
>> Chain[,2] is the column with all the samples for variable 2, etc.
>>    
>   The previous 2 paragraphs seemed contradictory until I realized
> that in the first paragraph you are using "run" to mean what I would
> usually call a "sample" ...
>
>  
>> I'd like to fit all 22 on a single page to print a nice summary.  It is
>> OK if the graphs are small, I just need to show the overall shape and
>> convergence.
>>    
>   How about for example
>
>  
>> x <- matrix(runif(22000),ncol=22)
>> library(coda)
>> m <- as.mcmc(x)
>> xyplot(m)
>> xyplot(m,layout=c(4,6))
>>    
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

djmuseR
Hi:

Ben's example worked for me as follows:

x <- matrix(runif(22000),ncol=22)
library(lattice)
library(coda)
m <- as.mcmc(x)
xyplot(m, layout = c(2, 11))

layout =  uses columns first, then rows...I always forget that :)  It also
has an optional
third argument for pages, which might come in handy here. If you want the
order of the
panels to be reversed, use the additional option as.table = TRUE.

HTH,
Dennis

On Tue, Jun 1, 2010 at 9:37 AM, Noah Silverman <[hidden email]>wrote:

> Hi,
>
> I used the term "run", as each iteration of the Gibbs sampler produces
> 22 variables (coefficients for Beta in a regression model)
>
>
> The example wont work
>
>
> On 6/1/10 5:54 AM, Ben Bolker wrote:
> > Noah Silverman <noah <at> smartmediacorp.com> writes:
> >
> >
> >> I'm running a long MCMC chain that is generating samples for 22
> variables.
> >>
> >> I have each run of the chain as a row in a matrix.
> >>
> >
> >> So:  Chain[,1] is the column with all the samples for variable one.
> >> Chain[,2] is the column with all the samples for variable 2, etc.
> >>
> >   The previous 2 paragraphs seemed contradictory until I realized
> > that in the first paragraph you are using "run" to mean what I would
> > usually call a "sample" ...
> >
> >
> >> I'd like to fit all 22 on a single page to print a nice summary.  It is
> >> OK if the graphs are small, I just need to show the overall shape and
> >> convergence.
> >>
> >   How about for example
> >
> >
> >> x <- matrix(runif(22000),ncol=22)
> >> library(coda)
> >> m <- as.mcmc(x)
> >> xyplot(m)
> >> xyplot(m,layout=c(4,6))
> >>
> > ______________________________________________
> > [hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

Noah Silverman-3
You are correct,

I initially missed the "as.mcmc" step.  Without it, R doesn't want to
squeeze so many plots onto a page.

Thanks!

On 6/1/10 10:48 AM, Dennis Murphy wrote:

> Hi:
>
> Ben's example worked for me as follows:
>
> x <- matrix(runif(22000),ncol=22)
> library(lattice)
> library(coda)
> m <- as.mcmc(x)
> xyplot(m, layout = c(2, 11))
>
> layout =  uses columns first, then rows...I always forget that :)  It
> also has an optional
> third argument for pages, which might come in handy here. If you want
> the order of the
> panels to be reversed, use the additional option as.table = TRUE.
>
> HTH,
> Dennis
>
> On Tue, Jun 1, 2010 at 9:37 AM, Noah Silverman
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Hi,
>
>     I used the term "run", as each iteration of the Gibbs sampler produces
>     22 variables (coefficients for Beta in a regression model)
>
>
>     The example wont work
>
>
>     On 6/1/10 5:54 AM, Ben Bolker wrote:
>     > Noah Silverman <noah <at> smartmediacorp.com
>     <http://smartmediacorp.com>> writes:
>     >
>     >
>     >> I'm running a long MCMC chain that is generating samples for 22
>     variables.
>     >>
>     >> I have each run of the chain as a row in a matrix.
>     >>
>     >
>     >> So:  Chain[,1] is the column with all the samples for variable one.
>     >> Chain[,2] is the column with all the samples for variable 2, etc.
>     >>
>     >   The previous 2 paragraphs seemed contradictory until I realized
>     > that in the first paragraph you are using "run" to mean what I would
>     > usually call a "sample" ...
>     >
>     >
>     >> I'd like to fit all 22 on a single page to print a nice
>     summary.  It is
>     >> OK if the graphs are small, I just need to show the overall
>     shape and
>     >> convergence.
>     >>
>     >   How about for example
>     >
>     >
>     >> x <- matrix(runif(22000),ncol=22)
>     >> library(coda)
>     >> m <- as.mcmc(x)
>     >> xyplot(m)
>     >> xyplot(m,layout=c(4,6))
>     >>
>     > ______________________________________________
>     > [hidden email] <mailto:[hidden email]> 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.
>     >
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot multiple columns

Noah Silverman-3
Another thought I had was to use Sweave.

I could, in theory, use Sweave to create a plot only 7 inches high, and
then a summary table 4 inches high below it, all on the same page of a
Latex document.  Could be messy, but might work.



On 6/1/10 11:18 AM, Dennis Murphy wrote:

>
>
> On Tue, Jun 1, 2010 at 10:51 AM, Noah Silverman
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     You are correct,
>
>     I initially missed the "as.mcmc" step.  Without it, R doesn't want
>     to squeeze so many plots onto a page.
>
>
> I've had that problem before with lattice...which makes me wonder
> whether you'll be able to put the plots
> and table on a single page. Here are some haphazard thoughts:
>
>   * The gplots package has functions textplot and sinkplot which IIRC
> will allow you to add a table to a plot.
>      I think you need to use layout() in base graphics to set it up
> properly, but you'd need to get 22 graphs
>      and a table on one graphics page...
>   * Given the number of graphs you want per page, perhaps sparkline
> plots might be less wasteful
>     of space (see the YaleToolkit package for one approach to sparklines)
>   * The problem is the time it would take to coordinate all of this...
>   * In a similar vein, there's always grid... :)
>
> Hopefully Deepayan or Felix Andrews can share some of their Lattice
> wisdom and show us how to
> do what you want in their system. That would be the most convenient
> solution.
>
> Dennis
>
>
>     Thanks!
>
>
>     On 6/1/10 10:48 AM, Dennis Murphy wrote:
>>     Hi:
>>
>>     Ben's example worked for me as follows:
>>
>>     x <- matrix(runif(22000),ncol=22)
>>     library(lattice)
>>     library(coda)
>>     m <- as.mcmc(x)
>>     xyplot(m, layout = c(2, 11))
>>
>>     layout =  uses columns first, then rows...I always forget that
>>     :)  It also has an optional
>>     third argument for pages, which might come in handy here. If you
>>     want the order of the
>>     panels to be reversed, use the additional option as.table = TRUE.
>>
>>     HTH,
>>     Dennis
>>
>>     On Tue, Jun 1, 2010 at 9:37 AM, Noah Silverman
>>     <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>         Hi,
>>
>>         I used the term "run", as each iteration of the Gibbs sampler
>>         produces
>>         22 variables (coefficients for Beta in a regression model)
>>
>>
>>         The example wont work
>>
>>
>>         On 6/1/10 5:54 AM, Ben Bolker wrote:
>>         > Noah Silverman <noah <at> smartmediacorp.com
>>         <http://smartmediacorp.com>> writes:
>>         >
>>         >
>>         >> I'm running a long MCMC chain that is generating samples
>>         for 22 variables.
>>         >>
>>         >> I have each run of the chain as a row in a matrix.
>>         >>
>>         >
>>         >> So:  Chain[,1] is the column with all the samples for
>>         variable one.
>>         >> Chain[,2] is the column with all the samples for variable
>>         2, etc.
>>         >>
>>         >   The previous 2 paragraphs seemed contradictory until I
>>         realized
>>         > that in the first paragraph you are using "run" to mean
>>         what I would
>>         > usually call a "sample" ...
>>         >
>>         >
>>         >> I'd like to fit all 22 on a single page to print a nice
>>         summary.  It is
>>         >> OK if the graphs are small, I just need to show the
>>         overall shape and
>>         >> convergence.
>>         >>
>>         >   How about for example
>>         >
>>         >
>>         >> x <- matrix(runif(22000),ncol=22)
>>         >> library(coda)
>>         >> m <- as.mcmc(x)
>>         >> xyplot(m)
>>         >> xyplot(m,layout=c(4,6))
>>         >>
>>         > ______________________________________________
>>         > [hidden email] <mailto:[hidden email]> 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.
>>         >
>>
>>         ______________________________________________
>>         [hidden email] <mailto:[hidden email]> 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.
>>
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.