David Afshartous
2008-Jun-26 14:26 UTC
[R] Connecting lines across missing data points, xyplot
All, I have data across 5 time points that I am graphing via xyplot, along with error bars. For one of the variables I have missing data for two of the time points. The code below is okay but I can't seem to get the lines to connect across the missing time points. Does anyone now how to rectify this? Cheers, David Afshartous library(lattice) ## the data junk = data.frame( Visit = as.factor(rep(seq(1,5), 2)), Drug = rep(c("D", "P"), each = 5), Aldo = c(13, NA, NA, 15, 14, 12, NA, NA, 14, 13), SE.Aldo = c(3, NA, NA, 3, 3, 2, NA, NA, 2, 2), lower.ci.Aldo = c(10, NA, NA, 12, 11, 10,NA, NA, 12, 11), upper.ci.Aldo = c(16, NA, NA, 18, 17, 14, NA, NA, 16, 15) ) ## functions for the error bars prepanel.ci <- function(x, y, ly, uy, subscripts, ...) { x <- as.numeric(x) ly <- as.numeric(ly[subscripts]) uy <- as.numeric(uy[subscripts]) list(ylim = range(y, uy, ly, finite = TRUE)) } panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) { x <- as.numeric(x) y <- as.numeric(y) ly <- as.numeric(ly[subscripts]) uy <- as.numeric(uy[subscripts]) panel.arrows(x, ly, x, uy, col = "black", length = 0.25, unit = "native", angle = 90, code = 3) panel.xyplot(x, y, pch = 16, ...)} ## the plot, but visit 1 not connected to visit 4 xyplot(Aldo ~ as.numeric(Visit), xlab="Visit", ylab="Aldo", groups=Drug, data=junk, ly = junk$lower.ci.Aldo, uy = junk$upper.ci.Aldo, prepanel = prepanel.ci, panel = panel.superpose, panel.groups = panel.ci, type="b", auto.key = list(space = "top", text = c( "D","P"), points = FALSE, lines = TRUE, columns=2), par.settings = list(superpose.line = list(lty = c(1,5), col=c('black', 'black') ) ) )
Doran, Harold
2008-Jun-26 16:02 UTC
[R] Connecting lines across missing data points, xyplot
Maybe approx() will work?> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of David Afshartous > Sent: Thursday, June 26, 2008 10:26 AM > To: r-help at r-project.org > Subject: [R] Connecting lines across missing data points, xyplot > > > > All, > > I have data across 5 time points that I am graphing via > xyplot, along with error bars. For one of the variables I > have missing data for two of the time points. The code below > is okay but I can't seem to get the lines to connect across > the missing time points. Does anyone now how to rectify this? > > Cheers, > David Afshartous > > > > library(lattice) > ## the data > junk = data.frame( > Visit = as.factor(rep(seq(1,5), 2)), > Drug = rep(c("D", "P"), each = 5), > Aldo = c(13, NA, NA, 15, 14, 12, NA, NA, 14, 13), SE.Aldo = > c(3, NA, NA, 3, 3, 2, NA, NA, 2, 2), lower.ci.Aldo = c(10, > NA, NA, 12, 11, 10,NA, NA, 12, 11), upper.ci.Aldo = c(16, > NA, NA, 18, 17, 14, NA, NA, 16, 15) > ) > > ## functions for the error bars > prepanel.ci <- function(x, y, ly, uy, subscripts, ...) { > x <- as.numeric(x) > ly <- as.numeric(ly[subscripts]) > uy <- as.numeric(uy[subscripts]) > list(ylim = range(y, uy, ly, finite = TRUE)) } panel.ci > <- function(x, y, ly, uy, subscripts, pch = 16, ...) { > x <- as.numeric(x) > y <- as.numeric(y) > ly <- as.numeric(ly[subscripts]) > uy <- as.numeric(uy[subscripts]) > panel.arrows(x, ly, x, uy, col = "black", > length = 0.25, unit = "native", > angle = 90, code = 3) > panel.xyplot(x, y, pch = 16, ...)} > > > ## the plot, but visit 1 not connected to visit 4 xyplot(Aldo > ~ as.numeric(Visit), xlab="Visit", ylab="Aldo", > groups=Drug, > data=junk, > ly = junk$lower.ci.Aldo, > uy = junk$upper.ci.Aldo, > prepanel = prepanel.ci, > panel = panel.superpose, > panel.groups = panel.ci, > type="b", > auto.key = list(space = "top", text = c( "D","P"), > points = FALSE, lines = TRUE, columns=2), par.settings = > list(superpose.line = list(lty = c(1,5), col=c('black', > 'black') ) ) ) > > ______________________________________________ > 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. >
Deepayan Sarkar
2008-Jun-26 21:29 UTC
[R] Connecting lines across missing data points, xyplot
On 6/26/08, David Afshartous <dafshartous at med.miami.edu> wrote:> > > All, > > I have data across 5 time points that I am graphing via xyplot, along with > error bars. For one of the variables I have missing data for two of the > time points. The code below is okay but I can't seem to get the lines to > connect across the missing time points. Does anyone now how to rectify > this?Well, I don't think joining lines ignoring NA-s would be a good idea in general. If you want that behaviour, you need to omit the NA's, either using junk <- na.omit(junk) before the xyplot call, or add subset = !is.na(Aldo), to the xyplot call. -Deepayan> > Cheers, > David Afshartous > > > > library(lattice) > ## the data > junk = data.frame( > Visit = as.factor(rep(seq(1,5), 2)), > Drug = rep(c("D", "P"), each = 5), > Aldo = c(13, NA, NA, 15, 14, 12, NA, NA, 14, 13), > SE.Aldo = c(3, NA, NA, 3, 3, 2, NA, NA, 2, 2), > lower.ci.Aldo = c(10, NA, NA, 12, 11, 10,NA, NA, 12, 11), > upper.ci.Aldo = c(16, NA, NA, 18, 17, 14, NA, NA, 16, 15) > ) > > ## functions for the error bars > prepanel.ci <- function(x, y, ly, uy, subscripts, ...) { > x <- as.numeric(x) > ly <- as.numeric(ly[subscripts]) > uy <- as.numeric(uy[subscripts]) > list(ylim = range(y, uy, ly, finite = TRUE)) } > panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) { > x <- as.numeric(x) > y <- as.numeric(y) > ly <- as.numeric(ly[subscripts]) > uy <- as.numeric(uy[subscripts]) > panel.arrows(x, ly, x, uy, col = "black", > length = 0.25, unit = "native", > angle = 90, code = 3) > panel.xyplot(x, y, pch = 16, ...)} > > > ## the plot, but visit 1 not connected to visit 4 > xyplot(Aldo ~ as.numeric(Visit), xlab="Visit", ylab="Aldo", > groups=Drug, > data=junk, > ly = junk$lower.ci.Aldo, > uy = junk$upper.ci.Aldo, > prepanel = prepanel.ci, > panel = panel.superpose, > panel.groups = panel.ci, > type="b", > auto.key = list(space = "top", text = c( "D","P"), points = FALSE, > lines = TRUE, columns=2), > par.settings = list(superpose.line = list(lty = c(1,5), col=c('black', > 'black') ) ) ) > > ______________________________________________ > 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. >
Carl Witthoft
2008-Jun-28 16:30 UTC
[R] Connecting lines across missing data points, xyplot
Just had to comment that I thought this question was unintentionally funny, seeing as many of us have gone thru a lot of pain to force Excel NOT to join lines across NA-type points in our data. Back to the subject: if you use is.na(), just be careful to remove the corresponding values in the x-data as well as the y-data :-) . Probably mucking with something like >if (y[j]=NA) y[j]<-approx({local x and y data fit})
Possibly Parallel Threads
- use xyplot to plot mean and CI by groups
- error bars in lattice xyplot *with groups*
- Error Bars in lattice- barcharts
- Error bars within xyplot, panel = function(x,y, ....)
- Equal confidence interval arrowhead lengths across multiple-paneled lattice plots with free y-scales