R Dot Plot With 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 ggplot2 error bars and policies of this site About Us Learn more about Stack Overflow
Summaryse R
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation error bars in r 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
Barplot With Error Bars R
only takes a minute: Sign up Scatter plot with error bars up vote 21 down vote favorite 11 How can I generate the following plot in R? Points, shown in the plot are the averages, and their ranges correspond to minimal and maximal values. I have data in two files (below is an example). x y 1 0.8773 1 0.8722 1 r dot plot with confidence intervals 0.8816 1 0.8834 1 0.8759 1 0.8890 1 0.8727 2 0.9047 2 0.9062 2 0.8998 2 0.9044 2 0.8960 .. ... r plot share|improve this question edited Oct 23 '12 at 15:10 Roland 74.2k463103 asked Oct 23 '12 at 14:29 sherlock85 1521313 Since you clearly don't want a boxplot, I changed the title of your question in order to reflect what you really want. –Roland Oct 23 '12 at 15:11 1 also plotrix::plotCI, gplots::plotCI, library("sos"); findFn("{error bar}") –Ben Bolker Oct 23 '12 at 17:29 add a comment| 5 Answers 5 active oldest votes up vote 52 down vote accepted First of all: it is very unfortunate and surprising that R cannot draw error bars "out of the box". Here is my favourite workaround, the advantage is that you do not need any extra packages. The trick is to draw arrows (!) but with little horizontal bars instead of arrowheads (!!!). This not-so-straightforward idea comes from the R Wiki Tips and is reproduced here as a worked-out example. Let's assume you have a vector of "average
error bars in R A colleague recently presented the results of a survey we designed. Instead of a table with means, standard deviations, t statistics and p values I thought a series of dotcharts with error
Calculate Standard Error In R
bars would be more appropriate and convey the required information in a
Stat_summary
more engaging manner. It wasn't as straight forward as I thought it should be to make such r dotchart plots comparing the results of different surveys, so I'm sharing my code. Some difficulties I ran into: - reordering the categories on the y axis (default is alphabetical http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars order, which is fine when they're called "Survey 1", "Survey 2" and "Survey 3" but not so good with more meaningful labels). - changing the text size, lattice graphics have some peculiar syntax compared with base or ggplot2 - getting the bars (with tips) to work nicely - adjusting the spacing so that the top and bottom surveys http://garthtarr.com/dotchart-with-error-bars-in-r/ weren't so close to the edge of the plot - labelling the x axis nicely - lattice graphics require the print function wrapper when called in a loop mypanel.Dotplot <- function(x, y, ...) { panel.Dotplot(x,y,...) tips = attr(x, "other") panel.arrows(x0 = tips[,1], y0 = y, x1 = tips[,2], y1 = y, length = 0.05, unit = "native", angle = 90, code = 3,lwd=2,col="blue") } library(Hmisc) dcfn = function(means,sds,n,title){ data = data.frame(ID=factor(c("Survey 1","Survey 2","Survey 3")), means=means, stderrs=2*sds/sqrt(n)) data$lower = data$means-data$stderrs data$upper = data$means+data$stderrs Dotplot(data$ID ~ Cbind(data$means,data$lower,data$upper), col="blue", pch=20, panel = mypanel.Dotplot, ylim=c(0.5,3.5), xlab=list("",cex=1.5), ylab=list("",cex=1.5),xlim=c(-0.5,10.5), cex=2, scales=list(y=list(cex=1.5, at=1:3, # for reordering the y labels: labels=levels(data$ID)[ c(1,3,2) ])), x = list(cex=1.5,at=c(0,5,10), labels=c("0nNo knowledge","5nModerate knowledge", "10nFull knowledge")), main=list(title,cex=1.5)) } n = c(107,45,54) means = c(3.4,7.1,6.6) sds = c(2.8,2.6,3.4) png("dotchart.png",width=1200,height=260) dcfn(means,sds,n=n,title="Question goes here") dev.off() # if using a loop you'll need to use the print function: # print(dcfn(means[i,],sds[i,],n=n,title=titles[i])) The result: By Garth| 2016-10-15T05:47:43+00:00 October 19th, 2014|R|0 Comments Share This Story, Choose Your Platform! FacebookTwitterLinkedinRedditGoogle+Email Leave A Comment Cancel reply Comment Notify me of follow-up comm
a GitHub account Sign in Create a gist now Instantly share code, notes, https://gist.github.com/tomhopper/9674890 and snippets. Star 0 Fork 0 tomhopper/ordered_dotplot_errorbars.R Last active Aug 29, 2015 Embed What would you like to do? Embed Embed this gist in your website. Embed Share Copy sharable URL for this gist. Share Clone via HTTPS Clone with Git or checkout with SVN using the repository's error bars web address. HTTPS Learn more about clone URLs Download ZIP Code Revisions 2 An ordered dot plot with error bars. Demonstrates creating summarized data (e.g. means), ordering by second variable, error bars on dot plots. Based on the bar chart example at http://martinsbioblogg.wordpress.com/2014/03/19/using-r-barplot-with-ggplot2/ Raw ordered_dotplot_errorbars.R #' Example Cleveland-style r dot plot dot plot. #' Two variables plotted on an facet plot. #' Ordering of the data in the first facet. #' Error bars plotted with each dot. library(ggplot2) library(reshape2) library(plyr) #' Create some data for the demonstration n <- 10 group <- rep(1:4, n) mass.means <- c(10, 20, 15, 30) mass.sigma <- 4 score.means <- c(5, 5, 7, 4) score.sigma <- 3 mass <- as.vector(model.matrix(~0+factor(group)) %*% mass.means) + rnorm(n*4, 0, mass.sigma) score <- as.vector(model.matrix(~0+factor(group)) %*% score.means) + rnorm(n*4, 0, score.sigma) data <- data.frame(id = 1:(n*4), group, mass, score) #' Reorder the data on "group," sorted by "mass" data <- transform(data, group = reorder(group, mass)) #' Change from wide to tall format for ggplot2 melted <- melt(data, id.vars=c("id", "group")) #' Calculate means and standard deviations means <- ddply(melted, c("group", "variable"), summarise, mean=mean(value)) means.sem <- ddply(melted, c("group", "variable"), summarise, mean=mean(value), sem=sd(value)/sqrt(length(value))) #' Create the da