[R] Loop for multiple plots in figure
baptiste auguie
baptiste.auguie at gmail.com
Tue Jun 26 11:38:10 CEST 2012
Try this alternative solution using only base functions:
# split the data into 4 data.frames
l <- split(data, data$Subject)
names(l)
# set up the graph parameters
par(mfrow=n2mfrow(length(l)), mar=c(4,4,1,1), mgp = c(2, 1, 0))
# good old for loop over the subject names
for( n in names(l)){
d <- l[[n]] # temporary data.frame for convenience
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
title(n) # here n is just a string
}
HTH,
b.
On 25 June 2012 23:45, Marcel Curlin <cemarcel at u.washington.edu> wrote:
> This solution works really nicely & I learned much by working through it.
> However but I am having trouble with subplot formatting; setting
> main=d$Subject results in the correct title over each plot but repeated
> multiple times. Also I can't seem to format the axis labels and numbers to
> reduce the space between them and the plot. Any more thoughts appreciated.
>
> revised code:
>
> tC <- textConnection("
> Subject Xvar Yvar param1 param2
> bob 9 100 1 100
> bob 0 110 1 200
> steve 2 250 1 50
> bob -5 175 0 35
> dave 22 260 0 343
> bob 3 180 0 74
> steve 1 290 1 365
> kevin 5 380 1 546
> bob 8 185 0 76
> dave 2 233 0 343
> steve -10 230 0 556
> dave -10 233 1 400
> steve -7 250 1 388
> dave 3 568 0 555
> kevin 10 380 0 57
> kevin 4 390 0 50
> bob 6 115 1 600
> ")
> data <- read.table(header=TRUE, tC)
> close.connection(tC)
> rm(tC)
>
> plot_one <- function(d){
> with(d, plot(Xvar, Yvar, t="n", tck=0.02, main=d$Subject, xlim=c(-14,14),
> ylim=c(0,600))) # set limits
> with(d[d$param1 == 0,], points(Xvar, Yvar, col = 1)) # first line
> with(d[d$param1 == 1,], points(Xvar, Yvar, col = 2)) # second line
>
> }
>
> par(mfrow=c(2,2))
> plyr::d_ply(data, "Subject", plot_one)
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Loop-for-multiple-plots-in-figure-tp4634390p4634482.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