[R] How do I plot geoms in parallel in ggplot

Charlotte Wickham
2010-05-06T18:17:51Z

You'll have to reshape your data so that each row corresponds to a
single subject, time and measurement.  I.e. something like:
Subj        Time         variable         value
1        1        Height        9
1        1        Weight       4
1        1        WBC          4
1        1        Plt            150
1        2        Height       10
1        2        Weight       5
....

This is easy enough with reshape.  Assuming your data is in data.frame df:
df.m <- melt(df, id.vars = c("Subj", "Time"))

Then plot:
qplot(Time, value, data = df, geom = "line")  + facet_grid(Subj ~
variable)


Charlotte

On Thu, May 6, 2010 at 11:06 AM, George Chen <glchen at stanford.edu>
wrote:
>
> Hello,
>
> I am new to ggplot. ?Please forgive my ignorance!
>
> I have patient data such that each individual is a row and then the
attributes are in columns. ?So for example:
>
> Subj ? ? ? ?Time ? ? ? ?Height ? ? ? ?Weight ? ? ? ?WBC ? ? ? ?Plt
> 1 ? ? ? ?1 ? ? ? ?9 ? ? ? ?4 ? ? ? ?4 ? ? ? ?150
> 1 ? ? ? ?2 ? ? ? ?10 ? ? ? ?5 ? ? ? ?6 ? ? ? ?200
> 1 ? ? ? ?3 ? ? ? ?11 ? ? ? ?6 ? ? ? ?5 ? ? ? ?250
> 1 ? ? ? ?4 ? ? ? ?12 ? ? ? ?7 ? ? ? ?3 ? ? ? ?200
> 2 ? ? ? ?1 ? ? ? ?4 ? ? ? ?2 ? ? ? ?7 ? ? ? ?300
> 2 ? ? ? ?2 ? ? ? ?5 ? ? ? ?4 ? ? ? ?8 ? ? ? ?450
> 2 ? ? ? ?3 ? ? ? ?6 ? ? ? ?6 ? ? ? ?6 ? ? ? ?300
> 2 ? ? ? ?4 ? ? ? ?8 ? ? ? ?8 ? ? ? ?5 ? ? ? ?200
> 3 ? ? ? ?1 ? ? ? ?6 ? ? ? ?6 ? ? ? ?4 ? ? ? ?455
> 3 ? ? ? ?2 ? ? ? ?8 ? ? ? ?8 ? ? ? ?5 ? ? ? ?344
> 3 ? ? ? ?3 ? ? ? ?9 ? ? ? ?9 ? ? ? ?6 ? ? ? ?544
> 3 ? ? ? ?4 ? ? ? ?10 ? ? ? ?10 ? ? ? ?3 ? ? ? ?213
>
> I would like to show the data as a time course for height , weight, WBC,
and plt for each patient but in parallel and ultimately as a matrix of time
courses for all the subjects with each patient a row and the time course graph
for height weight WBC plt as individual columns.
>
> So something like this
> Subj 1 ? Height time course(TC) ? Weight TC ? ?WBC TC ? ?Plt TC
> Subj 2 ? Height time course(TC) ? Weight TC ? ?WBC TC ? ?Plt TC
> Subj 3 ? Height time course(TC) ? Weight TC ? ?WBC TC ? ?Plt TC
>
> Ultimately I would like to be able to sort and reorder the order of the
subject rows (so each row would become like a facet).
> Could somebody give me a hint as to how or where to start looking to figure
this out? ?I suspect it involves a layer for each time course, but how does one
move the time course for say weight over so that it does not overly that for
height?
>
> Thanks in advance!
>
> George
>
> ______________________________________________
> 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.
>