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.
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.
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.
You can use main = unique(d$Subject) to solve this problem. HTH, b. On 27 June 2012 08:49, Marcel Curlin <cemarcel at u.washington.edu> wrote:> Well at this point I have what I need (rough plot for data exploration) but > the simplicity of the first approach is quite elegant and it has become a > learning project. I have succeeded in formatting the overall plot OK but > have not been able to solve the problem of titles or any kind of > label/legend for the subplots. It seems that the title is called for each > datapoint, and then printed one below the other in the plot. Is there any > way at all to get a specific legend/title/text on each subplot? > > Marcel > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Loop-for-multiple-plots-in-figure-tp4634390p4634649.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.
Seemingly Similar Threads
- while loop until end of file
- multiplying values in data frame by corresponding value in the first column
- XML parameters to Column Headers for importing into a dataset
- link_to and GET parameters
- MLE Estimation of Gamma Distribution Parameters for data with 'zeros'