Dot Plot With Error Bars In R
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 site About Us r lattice dotplot error bars Learn more about Stack Overflow the company Business Learn more about hiring developers or
Scatter Plot With Error Bars In R
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow barplot with error bars r 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 Scatter plot with error bars up vote 21
Ggplot2 Error Bars
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 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 .. ... summaryse r r plot share|improve this question edited Oct 23 '12 at 15:10 Roland 73.2k463102 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 51 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 values" avg and another vector of "standard deviations" sdev, they are of the same length n. Let's make the abscissa just the number of these "measurements", so x <- 1:n. Using these, here come the plotting commands: plot(x, avg, ylim=range(c(avg-sdev, avg+sdev)), pch=19, xlab="Measur
by over 573 bloggers. There are many ways to follow us
Calculate Standard Error In R
- By e-mail: On Facebook: If you are an R blogger
Ggplot Confidence Interval
yourself you are invited to add your own R content feed to this site (Non-English R bloggers stat_summary should add themselves- here) Jobs for R-usersFinance Manager @ Seattle, U.S.Data Scientist – AnalyticsTransportation Market Research Analyst @ Arlington, U.S.Data AnalystData Scientist for Madlan @ Tel Aviv, http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars Israel Popular Searches web scraping heatmap twitter maps time series boxplot animation shiny how to import image file to R hadoop Ggplot2 trading latex finance eclipse excel quantmod sql googlevis PCA knitr rstudio ggplot market research rattle regression coplot map tutorial rcmdr Recent Posts RcppAnnoy 0.0.8 R code to accompany Real-World Machine Learning (Chapter https://www.r-bloggers.com/r-tutorial-add-confidence-intervals-to-dotchart/ 2) R Course Finder update ggplot2 2.2.0 coming soon! All the R Ladies One Way Analysis of Variance Exercises GoodReads: Machine Learning (Part 3) Danger, Caution H2O steam is very hot!! R+H2O for marketing campaign modeling Watch: Highlights of the Microsoft Data Science Summit A simple workflow for deep learning gcbd 0.2.6 RcppCNPy 0.2.6 Using R to detect fraud at 1 million transactions per second Introducing the eRum 2016 sponsors Other sites Jobs for R-users SAS blogs R Tutorial: Add confidence intervals to dotchart May 15, 2011By Larry D'Agostino (This article was first published on Maximize Productivity with Industrial Engineer and Operations Research Tools, and kindly contributed to R-bloggers) Recently I was working on a data visualization project. I wanted to visualize summary statistics by category of the data. Specifically I wanted to see a simple dispersion of data with confidence intervals for each category of data. R is my tool of choice for data visualization. My audience was a general audience
a GitHub account Sign in Create a gist now Instantly share code, notes, and snippets. Star 0 Fork 0 tomhopper/ordered_dotplot_errorbars.R Last active Aug 29, 2015 Embed What would you like to do? https://gist.github.com/tomhopper/9674890 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 web address. HTTPS Learn more http://www.ats.ucla.edu/stat/r/pages/ggplot2_errorbar.htm 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 error bars bar chart example at http://martinsbioblogg.wordpress.com/2014/03/19/using-r-barplot-with-ggplot2/ Raw ordered_dotplot_errorbars.R #' Example Cleveland-style 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) plot with error 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 data frame that we will use for the error bars. means.sem <- transform(means.sem, lower=mean-sem, upper=mean+sem) #' Note that the order of geoms matters: plot geom_errorbarh first so that the points #' overlay the error bars. Reversing these plots the error bars over the dots, resulting #' in a visible gray line through each dot. #' Note that each geom can work with a different data set than the default passed to #' ggplot(). means.barplot <- ggplot(data = means, aes(x = mean, y = group)) + geom_errorbarh(data=means.sem, aes(xmax=upper, xmin=lower), height = 0.15, colour = "grey50") + geom_point(size = 5) + facet_grid(facets= . ~ variable, scales="free_x") + theme(text = element_text(size = 18)) #' Plot to standard graphics window. mea
Jittered Data Points Using ggplot2 Version info: Code for this page was tested in R version 3.3.1 (2016-06-21)
On: 2016-08-26
With: doBy 4.5-14; MplusAutomation 0.6-4; knitr 1.12.3; Hmisc 3.17-4; Formula 1.2-1; fitdistrplus 1.0-7; reshape2 1.4.1; ggplot2 2.1.0; memisc 0.99.7-1; lattice 0.20-33; plyr 1.8.3; JMbayes 0.7-9; survival 2.39-4; nlme 3.1-128; MASS 7.3-45; lme4 1.1-11; Matrix 1.2-6 Let's say that we want to produce a graph of scores for males and females broken down by subject, with the raw data and mean and standard error bars in ggplot2. There are four panels for four test score variables, math, read, science and write from 200 students. Each panel displays by gender an error bar overlaying jittered data points. We will show how to achieve this goal by steps using ggplot2. The data set for the example is hsb2 which can be downloaded from our server as shown below. For easily getting the statistics by group, we have used a package called "doBy", library(ggplot2) library(doBy) hsb2 <- read.table('http://www.ats.ucla.edu/stat/data/hsb2.csv', header=TRUE, sep=",") hsb2$female <- factor(hsb2$female, labels = c("male", "female")) Step 1. We make a data set in long format, so test scores are stacked. # collecting variables of interest for reshaping the data to long small <- hsb2[, c("write", "math", "read", "science", "female")] long <- reshape(small, varying=c("write", "math", "read", "science"), new.row.names = (1:800), times = c("write", "math", "read", "science"), v.names="y", direction="long", timevar="subject") head(long[order(long$id),], n=8) ## female subject y id ## 1 male write 52 1 ## 201 male math 41 1 ## 401 male read 57 1 ## 601 male science 47 1 ## 2 female write 59 2 ## 202 female math 53 2 ## 402 female read 68 2 ## 602 female science 63 2 Step 2. We create a data set containing summary statistics by gender and by subject using the summaryBy function from the package doBy. (a <- summaryBy(y ~ female + subject , long, FUN = c(mean, sd))) ## female subject y.mean y.sd ## 1 male math 52.9 9.66 ## 2 male read 52.8 10.51 ## 3 male science 53.2 10.73 ## 4 male write 50.1 10.31 #