Hi, I am new to R so forgive me if the following query is somewhat simple. I have a small tab-separated file with n records of glucose values, one record per day, seven measurements per day. It looks like this: date sober no vm nm va na vs 20091229 NA 6.8 NA 2.7 11.7 2.7 6.2 I'd like to make a graph on which the glucose day curves are plotted separately for each day. I'm sure I'll be able to make it pretty (such as labelling, etc), but yesterday I've been struggling just a bit too long to get the basics right. Here's what I've got till sofar: file = "d:/temp/glucose.tab" glucose <- read.table(file, header=TRUE, sep="\t", row.names="datum") # # Not sure if I got the row.names correct, I may need to use as.character (I believe I did use #that in the interactive session). attach(glucose) summary(glucose) ncol <- length(names(glucose)) xrange <- range(1, ncol) yrange <- range(0, max(!is.na(glucose[1:ncol]))) nrecs <- nrow(glucose) colors <- rainbow(nrecs) linetype <- c(1:nrecs) plot(xrange, yrange, type="n", xlab="Measurement moment", ylab="Glucose (mmol/liter)") for (i in 1: nrecs) { daily_values <- glucose[i,] lines(daily_values, type="b", lwd=1.5, lty=linetype[i], col=colors[i]) } So I want to loop over the file and add the plot lines one by one (day by day), but it seems that something's not yet right because nothing appears on the graph. Can anybody give me some pointers? Thank you in advance Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [[alternative HTML version deleted]]
I gather that each row defines one series, right? In that case try this: Lines <- "date sober no vm nm va na vs 20091229 NA 6.8 NA 2.7 11.7 2.7 6.2 20091230 NA 6.8 NA 2.7 11.7 2.7 6.2 20091231 NA 6.8 NA 2.7 11.7 2.7 6.2" # g <- read.table("myfile.txt", header = TRUE) g <- read.table(textConnection(Lines), header = TRUE) # create zoo object with each series in a column and plot it # na.approx fills in NAs with interpolated values library(zoo) z <- zoo(t(g[, -1])) colnames(z) <- g[,1] plot(na.approx(z), type = "b", main = "Glucose") If you want the series surperimposed on one panel then add the screens=1 argument to the last line. On Sun, Jan 3, 2010 at 3:24 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:> Hi, > > I am new to R so forgive me if the following query is somewhat simple.? I have a small tab-separated file with n records of glucose values, one record per day, seven measurements per day. It looks like this: > > date??? sober??? no??? vm??? nm??? va??? na??? vs > 20091229??? NA??? 6.8??? NA??? 2.7??? 11.7??? 2.7??? 6.2 > > I'd like to make a graph on which the glucose day curves are plotted separately for each day. I'm sure I'll be able to make it pretty (such as labelling, etc), but yesterday I've been struggling just a bit too long to get the basics right. Here's what I've got till sofar: > > file = "d:/temp/glucose.tab" > glucose <- read.table(file, header=TRUE, sep="\t", row.names="datum") # > # Not sure if I got the row.names correct, I may need to use as.character (I believe I did use #that in the interactive session). > attach(glucose) > summary(glucose) > > ncol <- length(names(glucose)) > xrange <- range(1, ncol) > yrange <- range(0, max(!is.na(glucose[1:ncol]))) > > nrecs <- nrow(glucose) > colors <- rainbow(nrecs) > linetype <- c(1:nrecs) > plot(xrange, yrange, type="n", xlab="Measurement moment",? ylab="Glucose (mmol/liter)") > for (i in 1: nrecs) { > ? daily_values <- glucose[i,] > ? lines(daily_values, type="b", lwd=1.5, lty=linetype[i], col=colors[i]) > } > > So I want to loop over the file and add the plot lines one by one (day by day), but it seems that something's not yet right because nothing appears on the graph. Can anybody give me some pointers? Thank you in advance > > > Cheers!! > > Albert-Jan > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > In the face of ambiguity, refuse the temptation to guess. > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >
Hi: My understanding is that you want a separate plot over time of each of your repeated measures variables. If that's the case, then perhaps something along the following lines will work. Given the manufactured data set Gabor provided (g from lines), let's use the reshape package and then plot using lattice and ggplot2: library(ggplot2) # attaches reshape along the way library(lattice) g2 <- melt(g, id = 'date') # lattice plot xyplot(value ~ date | variable, data = g3) # ggplot ggplot(g3, aes(date, value)) + geom_point() + facet_wrap(~ variable) The plots are less than publication quality, but the labels, etc. can be tweaked and you can change the layout of the graphics region if you wish. The advantage of melt() in this circumstance is that it allows one to use variable names as factor labels in lattice and ggplot2. HTH, Dennis On Sun, Jan 3, 2010 at 12:24 PM, Albert-Jan Roskam <fomcl@yahoo.com> wrote:> Hi, > > I am new to R so forgive me if the following query is somewhat simple. I > have a small tab-separated file with n records of glucose values, one record > per day, seven measurements per day. It looks like this: > > date sober no vm nm va na vs > 20091229 NA 6.8 NA 2.7 11.7 2.7 6.2 > > I'd like to make a graph on which the glucose day curves are plotted > separately for each day. I'm sure I'll be able to make it pretty (such as > labelling, etc), but yesterday I've been struggling just a bit too long to > get the basics right. Here's what I've got till sofar: > > file = "d:/temp/glucose.tab" > glucose <- read.table(file, header=TRUE, sep="\t", row.names="datum") # > # Not sure if I got the row.names correct, I may need to use as.character > (I believe I did use #that in the interactive session). > attach(glucose) > summary(glucose) > > ncol <- length(names(glucose)) > xrange <- range(1, ncol) > yrange <- range(0, max(!is.na(glucose[1:ncol]))) > > nrecs <- nrow(glucose) > colors <- rainbow(nrecs) > linetype <- c(1:nrecs) > plot(xrange, yrange, type="n", xlab="Measurement moment", ylab="Glucose > (mmol/liter)") > for (i in 1: nrecs) { > daily_values <- glucose[i,] > lines(daily_values, type="b", lwd=1.5, lty=linetype[i], col=colors[i]) > } > > So I want to loop over the file and add the plot lines one by one (day by > day), but it seems that something's not yet right because nothing appears on > the graph. Can anybody give me some pointers? Thank you in advance > > > Cheers!! > > Albert-Jan > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > In the face of ambiguity, refuse the temptation to guess. > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]