[R] Loop for multiple plots in figure

baptiste auguie baptiste.auguie at googlemail.com
Mon Jun 25 09:47:25 CEST 2012


Hi,

Here's one approach:

plot_one <- function(d){
  with(d, plot(Xvar, Yvar, t="n")) # set limits
  with(d[d$param1 == 0,], lines(Xvar, Yvar, lty=1)) # first line
  with(d[d$param1 == 1,], lines(Xvar, Yvar, lty=2)) # second line

}

par(mfrow=c(2,2))
plyr::d_ply(data, "Subject", plot_one)

HTH,

b.

On 24 June 2012 23:06, Marcel Curlin <cemarcel at u.washington.edu> wrote:
> Hello, I have longitudinal data of the form below from N subjects; I am
> trying to create figure with N small subplots on a single page, in which
> each plot is from only one subject, and in each plot there is a separate
> curve for each value of param1.
>
> So in this case, there would be four plots on the page (one each for Bob,
> Steve, Kevin and Dave), and each plot would have two separate curves (one
> for param1 = 1 and one for param1 = 0). The main title of the plot should be
> the subject name. I also need to sort the order of the plots on the page by
> param2.
>
> I can do this with a small number of subjects using manual commands. For a
> larger number I know that a 'for loop' is called for, but can't figure out
> how to get each of the subjects to plot separately, could not figure it out
> from the existing posts.  For now I want to do this in the basic environment
> though I know that lattice could also work (might try that later). Any help
> appreciated
>
> tC <- textConnection("
> Subject Xvar    Yvar    param1  param2
> bob     9       100     1       100
> bob     0       250     1       200
> steve   2       454     1       50
> bob     -5      271     0       35
> bob     3       10      0       74
> steve   1       500     1       365
> kevin   5       490     1       546
> bob     8       855     0       76
> dave    2       233     0       343
> steve   -10     388     0       556
> steve   -7      284     1       388
> dave    3       568     1       555
> kevin   4       247     0       57
> bob     6       300     1       600
> ")
> data <- read.table(header=TRUE, tC)
> close.connection(tC)
> rm(tC)
>
> par(mfrow=c(2,2)
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Loop-for-multiple-plots-in-figure-tp4634390.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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