R Lattice Plot 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 and policies
Lattice Error Bars
of this site About Us Learn more about Stack Overflow the company lattice xyplot error bars Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users error bars in r 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")), t.ws = c(32.7, 35.5, 37.1, 37.6, 38.7), mean.tor
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow 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 R - Lattice xyplot - How do you add error bars to groups and summary lines? up vote 0 down vote favorite 1 I'm posting this http://stackoverflow.com/questions/19975390/add-error-bars-seperately-in-lattice-line-plot question because the very similar question here has not been answered until now. I have been asked to plot the mean +/- SEM of my whole cohort of patients over the xyplot() that depicts the values of all patients. The data used represents intraoperative cardiovascular findings from patients undergoing surgery. This is my data.frame called df dput(df) structure(list(Name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, http://stackoverflow.com/questions/26332489/r-lattice-xyplot-how-do-you-add-error-bars-to-groups-and-summary-lines 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DE", "JS", "KG", "MK", "TG", "WT"), class = "factor"), Time = structure(c(1L, 2L, 3L, 4L, 5L, 7L, 8L, 1L, 2L, 3L, 4L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 2L, 3L, 4L, 5L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8"), class = "factor"), Dobut = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("No", "Yes" ), class = "factor"), DobutDose = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 6L, 8L, 8L, 8L, 8L, 8L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, 5L, NA), CI = c(1.4, 2.3, 1.3, 1.8, 2.1, 2, 2.1, 2.1, 2.3, 1.9, 1.6, 2, 2.4, 2.7, 2.6, 2.7, 2.6, 2.3, 2.4, 2.6, 0.9, 2.5, 2.1, 1.6, 1.5, 1.8, 2, 2, 1.9, 2.1, 2.3, 2, 2.4, 2.3, 2.6, 2.4
this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ error bars in lattice xyplot *with groups* Hi all, I'm trying to http://r.789695.n4.nabble.com/error-bars-in-lattice-xyplot-with-groups-td801398.html plot error bars 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 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, error bars split(height, 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)) plot error bars #now let's split up the voice.part factor into 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,