[R] Plotting longitudinal data

Joshua Wiley jwiley.psych at gmail.com
Tue Sep 7 19:11:57 CEST 2010


Hi,

Reading in (and slightly editing your data, so that patient_id is a
factor, and the date is class date).  Results of dput() provided for
others' benefit.

dat <- structure(list(V1 = c("7213", "1", "4797", "2399", "2400", "7230",
"2", "4798", "4799", "2401", "3", "7233", "4", "4800", "2402",
"7258"), patient_id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("3333330001",
"3333330002", "3333330003", "3333330004"), class = "factor"),
    value = c(0.83, 0.87, 0.77, 0.78, NA, 0.82, 0.84, 0.87, 0.9,
    0.93, 0.84, NA, 0.72, 0.89, 0.73, 0.77), date = structure(c(14377,
    12823, 13593, 13250, 13234, 14378, 12823, 13592, 13592, 13234,
    12824, 14378, 12824, 13591, 13236, 14382), class = "Date"),
    V5 = c("1", "NULL", "NULL", "NULL", "NULL", "0", "NULL",
    "0", "13", "16", "NULL", "1", "NULL", "22", "27", "1")), .Names = c("V1",
"patient_id", "value", "date", "V5"), row.names = c(NA, -16L), class =
"data.frame")

Here are two simple little plots:

library(ggplot2)
ggplot(data = dat, aes(x = date, y = value, colour = patient_id)) + geom_line()

library(lattice)
xyplot(value ~ date, data = dat, groups = patient_id, type = "l")

Cheers,

Josh



On Tue, Sep 7, 2010 at 9:27 AM, Jukka Koskela <jukka.koskela at helsinki.fi> wrote:
> Hello,
>
> Hope that someone could help me plotting longitudinal data below:
>
> 7213    3333330001      0.8300  13.05.09        1
> 1       3333330001      0.8700  09.02.05        NULL
> 4797    3333330001      0.7700  21.03.07        NULL
> 2399    3333330001      0.7800  12.04.06        NULL
> 2400    3333330002      NULL    27.03.06        NULL
> 7230    3333330002      0.8200  14.05.09        0
> 2       3333330002      0.8400  09.02.05        NULL
> 4798    3333330002      0.8700  20.03.07        0
> 4799    3333330003      0.9000  20.03.07        13
> 2401    3333330003      0.9300  27.03.06        16
> 3       3333330003      0.8400  10.02.05        NULL
> 7233    3333330003      NULL    14.05.09        1
> 4       3333330004      0.7200  10.02.05        NULL
> 4800    3333330004      0.8900  19.03.07        22
> 2402    3333330004      0.7300  29.03.06        27
> 7258    3333330004      0.7700  18.05.09        1
>
> The second column is a patient_id, the third is the value I want to plot
> against the fourth which is the date.
>
> First I 'aggregate' the patient_ids:
>
> id<-unique(dat$patient_id)
>
> Then I try (and fail) to create a loop, that is supposed to plot the data:
>
> for(i in 1:8480){patient_id==id[i]plot(date,value)}
>
>
> What might be wrong?
>
> And how could I only plot eg quintiles for the ones that go down fastest?
>
> Thanks,
>
> Jukka
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list