[R] Connect lines in a dot plot on a subject-by-subject basis

Bert Gunter gunter.berton at gene.com
Wed Mar 28 23:47:19 CEST 2012


Trivial with xyplot.

Convert the data frame to long form (perhaps using reshape() or
functions in the reshape2 package) to get ( in data frame dat) 3
columns:

SubjectID  Time Value

(results do not have to be in time order or grouped by subject)

Then:

xyplot( Value ~ Time, Group = SubjectID, data = dat, type = "b")

(or type - "l" if you don't want the points)

-- Bert

On Wed, Mar 28, 2012 at 2:39 PM, John Sorkin
<jsorkin at grecc.umaryland.edu> wrote:
> I am trying to plot where data points from a give subject are connected by a line. Each subject is represented by a single row of data. Each subject can have
> up to five observations. The first five columns of mydata give the time of observation, columns 6-10 give the values at each time point. Some subjects have
> all data, some are missing values.
>
> The code I wrote to draw the plot is listed below. If you run the code you will see that (1) The few lines drawn are not drawn on a subject-by-subject basis, (2) not all lines are drawn.
>
> I hope someone can help me fix my code
>
> Thanks,
> John
>
> # Create a matrix to hold our data.
> mydata <- matrix(nrow=10,ncol=10)
> dimnames(mydata) <- list(NULL,c("time1", "time2", "time3", "time4", "time5",
>                                "clot1", "clot2", "clot3", "clot4", "clot5"))
> # load data into the matrix.
> # The first five columns represent time of measurement
> # Columns 6-10 have the measured values.
> mydata[1,]=c( 1, 3, NA,NA, NA, 22, 40, NA, NA, NA)
> mydata[2,]=c( 1, NA,NA,NA, NA, 39, NA, NA, NA, NA)
> mydata[3,]=c( 1, 6, NA,NA, NA, 47, 43, NA, NA, NA)
> mydata[4,]=c( 1, 3, 7, 21, NA, 34, 29, 33, 21, NA)
> mydata[5,]=c( NA,NA,NA,NA, NA, NA, NA, NA, NA, NA)
> mydata[6,]=c( 1, 3, 7, 19, 35, 42, 60, 86, 56, 39)
> mydata[7,]=c( 1, NA,NA,NA, NA, 57, NA, NA, NA, NA)
> mydata[8,]=c( 1, 3, 10,17, 25, 71, 55, 57, 62, 47)
> mydata[9,]=c( 1, NA,NA,NA, NA, 79, NA, NA, NA, NA)
> mydata[10,]=c( 1,3, 9, NA, NA, 60, 64, 68, NA, NA)
>
> # Dispaly data.
> mydata
>
> # Plot data.
> plot(mydata[,1:5],mydata[,6:10])
> # Attempt to connect points for each subject with lines.
> lines(mydata[,1:5],mydata[,6:10],type="b")
>
> John David Sorkin M.D., Ph.D.
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:22}}



More information about the R-help mailing list