[R] Plot multiple columns

baptiste auguie baptiste.auguie at googlemail.com
Tue Jun 1 12:22:28 CEST 2010


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 <noah at smartmediacorp.com> 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!
>
> ______________________________________________
> 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