Error Bars In R Lattice
Contents |
The Ridge Savanna science in South Africa Interviews Opinions Education This is how I did it… From the Field EcoPics Events NREL News Links Citation Submission Podcast This is how I did lattice xyplot error bars it … added error bars to a lattice bar chart inR How did
Error Bars In R Barplot
I even get here? A few months ago, I found myself deep in an R rabbit hole, googling into the error bars in r plot depths of the internet, stackoverflow, R help functions, maybe you’ve been there… all to answer a simple question: how do I add error bars to a barchart in lattice? Riddled with frustration, add error bars r I wondered how it could possibly be this difficult if every publication quality graphic needs error bars. We, in ecology, publish many a bar chart, shouldn’t it be easy to find a solution to this seemingly simple problem? Many folks that I asked (including my twitter followers, #rstats), just said to me, ‘Yamina, stop being silly, just make your plot in ggplot2! Error bars are so
Adding Error Bars In R
easy!’ This response (1) does not solve my problem and (2) would force me to give up some of the other nice lattice functionality (like panels and grouped bars) which is why I switched to lattice in the first place. (Case and point.) Honestly, it felt like I was turning in circles. After hours, days, and some serious debugging with coding experts, I finally figured out how to add error bars to my bar chart. Is it possible? Yes. Is it straight forward? No. But, the resulting graphics are surely publication quality and I ended up with a figure I am quite proud of. So, since it was such a headache to figure this out by piecing together the puzzle that is the internet, I figured the least I could do is share my solution here on EcoPress. For all those reading this post, I want you to know that I am far from an R expert and I don’t pretend to be. There may be a better (easier?), less code-intensive way to achieve the same end product. Please let us know in the comments section below if there is an easier way to do this
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 r lattice xyplot error bars 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 4.7 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 question https://nrelscience.org/2016/03/01/this-is-how-i-did-it-added-error-bars-to-a-lattice-bar-chart-in-r/ 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, 6L, 6L, http://stackoverflow.com/questions/26332489/r-lattice-xyplot-how-do-you-add-error-bars-to-groups-and-summary-lines 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, 2, 2.2, 1
[ date ] [ thread ] [ subject ] [ author ] Dear Deepayan and Sundar, Thank you so much for your help with this. However, I may have phrased my problem too specifically, assuming that *in general* I could apply https://stat.ethz.ch/pipermail/r-help/2006-October/114998.html your response to all Lattice graphics. What I need is a barchart or vertical dotchart, with http://www.elff.eu/knitr/memisc/errbars.html error bars, across three treatments, with a form that should look something like: barchart(median~fac1|by1, groups=group1). Your solution works great with the problem I had posed
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]