R 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 and policies of this lattice error bars site About Us Learn more about Stack Overflow the company Business Learn
Lattice Xyplot Error Bars
more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question error bars in r 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.tort = c(32.4, 34.9, 35.1, 36.8, 37.7), se.tort = c(0.825, 0.84, 0.21, 0
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 http://stackoverflow.com/questions/19975390/add-error-bars-seperately-in-lattice-line-plot 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- Add error bars to different groups in xyplot up vote 1 down vote favorite I'm quite new to R, so I hope you http://stackoverflow.com/questions/23678494/r-add-error-bars-to-different-groups-in-xyplot guys can help me out with this problem. I tried to look for other posts with similar questions, but wasn't able to find any. This is how my representative data set is constructed: library(lattice) data(Theoph) DF <- Theoph DF$Time <- rep(c(0,0.25,0.5,1,2,4,5,7,9,12,24), times=12) DF$group[DF$Subject==1|DF$Subject==2|DF$Subject==3] <- "A" DF$group[DF$Subject==4|DF$Subject==5|DF$Subject==6] <- "B" DF$group[DF$Subject==7|DF$Subject==8|DF$Subject==9] <- "C" DF$group[DF$Subject==10|DF$Subject==11|DF$Subject==12] <- "D" y <- DF$conc x <- DF$Time groups <- as.factor(DF$group) I made an xyplot that shows the individual data points and also lines showing the mean per group (A,B,C and D) using this code: xyplot(y ~ x, type="p", groups=groups, auto.key=T, panel = function(x, y, groups, subscripts, ...){ panel.superpose(x, y, groups, subscripts,..., panel.groups=function(x,y,col,col.symbol,...){ panel.xyplot(x, y, col=col.symbol,...) panel.average(x, y, col=col.symbol, lwd = 2, horizontal = FALSE) }) }) I would like to add error bars (e.g. +/- standard deviation) to the mean values. I know there is a function called panel.arrows, but I haven't been successful in
this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ error bars http://r.789695.n4.nabble.com/error-bars-in-lattice-xyplot-with-groups-td801398.html in lattice xyplot *with groups* Hi all, I'm trying to plot http://www.martin-elff.net/knitr/memisc/errbars.html 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 error bars this, consider the singer data generated by the above linked solution previously submitted: ##################### library(lattice) singer.split <- with(singer, split(height, voice.part)) singer.ucl <- sapply(singer.split, function(x) { st <- boxplot.stats(x) xyplot error bars 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 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",
x,y numeric values, the points around which error bars are plotted. x is a numeric vector, y is a matrix with three columns, the values, the lower and the upper ends of the error bars. ... graphical parameters passed to panel.xy panel.xy panel function to produce the plot to which error bars are added make.grid character string, determines the direction of grid lines added to the plot ewidth numerical value, width of the whiskers of the error bars See Also panel.xyplot Examples library(lattice) library(grid) ## Don't show: if(interactive()) old.prompt <- grid::grid.prompt(TRUE) ## Error: 'grid.prompt' is not an exported object from 'namespace:grid' ## End Don't show applications <- Aggregate(percent(Dept,weight=Freq,ci=TRUE)~Gender, data=UCBAdmissions) admissions <- Aggregate( percent(Admit=="Admitted",weight=Freq,ci=TRUE)~Dept+Gender, data=UCBAdmissions) xyplot(cbind(Percentage,lower,upper)~Gender|Dept,data=admissions, panel=panel.errbars, ewidth=.2,pch=19, ylab="Percentage applicants admitted by Department") xyplot(cbind(Percentage,lower,upper)~Gender|Dept,data=applications, panel=panel.errbars, ewidth=.2,pch=19, ylab="Percentage applications to the Department") ## Don't show: if(interactive()) grid::grid.prompt(old.prompt) ## Error: 'grid.prompt' is not an exported object from 'namespace:grid' ## End Don't show [Package memisc version 0.97 Index]