Lattice Xyplot Error Bars
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Lattice Error Bars
and policies of this site About Us Learn more about Stack Overflow error bars in r the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Add error bars seperately in lattice line-plot? up vote 0 down vote favorite 2 I have a line-plot produced with lattice's xyplot. It contains two types of temperatures measured, one of these are mean values. Therefore, I would like to add standard errors to these points (t.tort). Unfortunately, doing this seperately does not work using (Hmisc)errbar... here is what I have come up with so far: xyplot(mean.tort+t.ws~DateTime, pre, type=c("a", "p"), col=c("red", "blue"), main="Pre-Translocation", xlab=list(label="Date and Time", cex=1), ylab=list(label="Temperature (°C)", cex=1), scales = list(tck = c(1, 0), x=list(cex=0.8, rot=45, tick.number=40), y=list(cex=0.8, tick.number=8, limits=c(29,43))), key=list(text=list(c("Tortoise","Ambient")), lines=list(col=c("red", "blue"),type="l"), corner=c(0.5,0.92))) errbar(x=pre$DateTime, y=pre$mean.tort, yplus=pre$mean.tort+pre$se.tort,yminus=pre$mean.tort-pre$se.tort, add=T, col="red") And the important bits of my data frame are as follows: pre$DateTime<-c(as.POSIXct("2013-01-27 09:00:00" "2013-01-27 10:00:00" "2013-01-27 11:00:00" "2013-01-27 12:00:00" "2013-01-27 13:00:00") pre$t.ws<-c(32.7, 35.5, 37.1, 37.6, 38.7) pre$mean.tort<-c(32.4, 34.9, 35.1, 36.8, 37.7) pre$se.tort<-c(0.825, 0.84, 0.21, 0.228, 0.28) I got a bit frustrated with this, so any suggestions will be greatly appreaciated. Thank you very much for your efforts in advance! r lattice line-plot share|improve this question edited Nov 15 '13 at 7:42 asked Nov 14 '13 at 10:35 LeoTor 156 You can't mix base and grid graphics. Have you looked around? stackoverflow.com/questions/2381618/… –Roman Luštrik Nov 14 '13 at 12:21 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted Try this Your data: pre = data.frame(DateTime = as.POSIXct(c("2013-01-27 09:00:00", "2013-01-27 10:00:00", "2013-01-27 11:00:00", "2013-01-27 12:00:00", "2013-01-27 13:00:00")),
Density Plot Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] All, I'm trying to adapt some code provided by Deepayan Sarkar from a previous thread (https://stat.ethz.ch/pipermail/r-help/2005-October/081571.html) on this topic. ## This code produces a graph w/o error bars: xyplot(Y ~ Hr, data, groups=DRUG, panel=function(x,y,...){ panel.xyplot(x,y,..., type=c("g", "l") ) panel.points(x,y,..., pch=16, type='p', col='black', cex=1) }, auto.key = list(space = "top", text = c( "D","P"), points = FALSE, lines = TRUE, columns=2), par.settings = list(superpose.line = http://stackoverflow.com/questions/19975390/add-error-bars-seperately-in-lattice-line-plot list(lty = c(1,5), col=c('black', 'black') ) ) ) ## this code uses the functions provided by Deepayan Sarkar to include the ## error bars for the same data: xyplot(Y ~ Hr, groups=DRUG, data=data, ly = data$lower, uy = data$upper, prepanel = prepanel.ci, panel = panel.superpose, panel.groups = panel.ci, type="b", auto.key = list(space = "top", text = c( "D","P"), points https://stat.ethz.ch/pipermail/r-help/2008-June/164968.html = FALSE, lines = TRUE, columns=2), par.settings = list(superpose.line = list(lty = c(1,5), col=c('black', 'black') ) ) ) Is it possible to write the second version in the format of the first, i.e., using panel = function(x,y, ...){ } ? Thanks, David ############################################################################ ### load the following: Hr = c(0,1,2,3,4,5,0,1,2,3,4,5) DRUG = rep(c("D", "P"), each=6) Y = c(1,2,2,2,2,1,3, 4, 4,4, 4, 3) data = data.frame(Hr, DRUG, Y) data$lower = data$Y - .5 data$upper = data$Y + .5 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, ...)} Previous message: [R] x labels out of Quartz canvas Next message: [R] Superimposing Line over Histogram in Density Plot Messages sorted
this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ error bars in lattice xyplot *with groups* Hi all, I'm trying to plot error bars http://r.789695.n4.nabble.com/error-bars-in-lattice-xyplot-with-groups-td801398.html in a lattice plot generated with xyplot. Deepayan Sarkar has provided a very useful solution for simple circumstances (https://stat.ethz.ch/pipermail/r-help/2005-October/081571.html), yet I am having trouble getting it to work when the "groups" setting is http://grokbase.com/t/r/r-help/104nwm3dam/r-adding-error-bars-to-xyplot enabled in xyplot (i.e. multiple lines). To illustrate this, consider the singer data generated by the above linked solution previously submitted: ##################### library(lattice) singer.split <- with(singer, split(height, error bars voice.part)) singer.ucl <- sapply(singer.split, function(x) { st <- boxplot.stats(x) c(st$stats[3], st$conf) }) singer.ucl <- as.data.frame(t(singer.ucl)) names(singer.ucl) <- c("median", "lower", "upper") singer.ucl$voice.part <- factor(rownames(singer.ucl), levels = rownames(singer.ucl)) #now let's split up the voice.part factor into lattice xyplot error two factors, singer.ucl$voice=factor(rep(c(1,2),4)) singer.ucl$range=factor(rep(c("Bass","Tenor","Alto","Soprano"),each=2)) #here's Deepayan's previous solution, slightly modified to depict # the dependent variable (median) and the error bars on the y-axis # and the independent variable (voice.part) on the x-axis 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 = pch, ...) } #this graph works xyplot(median ~ voice.part, data=singer.ucl, ly = singer.ucl$lower, uy = singer.ucl$upper, prepanel = prepanel.ci, panel = panel.ci, type="b" ) #this one does not (it will plot,
and plotCI(), but in both cases the points are not in thesame place. It's as if the two functions are using a different frame ofreference for the plotting area.for example:means <- c(92.5, 92.25, 90.9, 91.0, 94.15, 90.05) #meanstime <- c(1,1,2,2,3,3) #occasion variablegroup <- rep(c("n","u"),3) #grouping variableSE <- c(2.22, 1.66, 2.10, 1.43, 2.31, 1.57) #standard errors#Plot the graphxyplot(means ~ time, groups=group, type="o", lwd=3, main="Change OverTime by Condition", scale=list(cex=2), xlab="Test Number", auto.key=T)#first attemptrequire(Hmisc)errbar(x = time, y = means, yplus = means + SE, yminus = means - SE,add=T, col=c("blue", "hotpink"))#second attempt, same resultrequire(gplots)plotCI(x = time, y = means, uiw = SE, add=T)Thanks in advance!- Jon--Jon ZadraDepartment of PsychologyUniversity of VirginiaP.O. Box 400400Charlottesville VA 22904(434) 982-4744email: zadra at virginia.edu<http://www.google.com/calendar/embed?src=jzadra%40gmail.com> reply Tweet Search Discussions Search All Groups r-help 4 responses Oldest Nested David Winsemius Another base graphics function. Since you already have Hmisc why not check out Harrell's xYplot which provides CI's in a grid function: From the xYplot examples: require(Hmisc) dfr <- expand.grid(month=1:12, continent=c('Europe','USA'), sex=c('female','male')) set.seed(1) dfr <- upData(dfr, y=month/10 + 1*(sex=='female') + 2*(continent=='Europe') + runif(48,-.15,.15), lower=y - runif(48,.05,.15), upper=y + runif(48,.05,.15)) xYplot(Cbind(y,lower,upper) ~ month|continent, David Winsemius at Apr 21, 2010 at 10:47 pm ⇧ On Apr 21, 2010, at 6:24 PM, Jon Zadra wrote:Hi,I want to add error bars to a plot generated with xyplot. I'vetried both errbar() and plotCI(), but in both cases the points arenot in the same place. It's as if the two functions are using adifferent frame of reference for the plotting area.Yes. plotCI uses base graphics while xyplot is a lattice function.for example:means <- c(92.5, 92.25, 90.9, 91.0, 94.15, 90.05) #meanstime <- c(1,1,2,2,3,3) #occasion variablegroup <- rep(c("n","u"),3) #grouping variableSE <- c(2.22, 1.66, 2.10, 1.43, 2.31, 1.57) #standard errors#Plot the graphxyplot(means ~ time, groups=group, type="o", lwd=3, main="ChangeOver Time by Condition", scale=list(cex=2), xlab="Test Number",auto.key=T)#first attemptrequire(Hmisc)errbar(x = time, y = means, yplus = means + SE, yminus = means - SE,add=T, col=c("blue", "hotpink"))A base graphics function.#second attempt, same resultrequire(gplots)plotCI(