Ggplot2 Dodge Error Bars
with wider geoms. See the examples for a use r calculate standard error case. Description Adjust position by dodging overlaps to
the side. Examples ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = "dodge") ggplot(diamonds, aes(price, summaryse fill = cut)) + geom_histogram(position="dodge") `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. # see ?geom_boxplot and ?geom_bar for error bars in r more examples # To dodge items with different widths, you need to be explicit df <- data.frame(x=c("a","a","b","b"), y=2:5, g = rep(1:2, 2)) p <- ggplot(df, aes(x, y, group = g)) + geom_bar( stat = "identity", position = "dodge", fill = "grey50", colour = "black"
Ggplot Confidence Interval
) p # A line range has no width: p + geom_linerange(aes(ymin = y-1, ymax = y+1), position = "dodge") Warning message: Width not defined. Set with `position_dodge(width = ?)` # You need to explicitly specify the width for dodging p + geom_linerange(aes(ymin = y-1, ymax = y+1), position = position_dodge(width = 0.9)) # Similarly with error bars: p + geom_errorbar(aes(ymin = y-1, ymax = y+1), width = 0.2, position = "dodge") p + geom_errorbar(aes(ymin = y-1, ymax = y+1, width = 0.2), position = position_dodge(width = 0.90)) See also Other position adjustments: position_fill, position_identity, position_jitterdodge, position_jitter, position_nudge Back to top What do you think of the documentation? Please let me know by filling out this short online survey. Built by staticdocs. Styled with bootstrap.
Sign in Pricing Blog Support Search GitHub This repository Watch 246 Star 2,065 Fork 780 hadley/ggplot2 Code Issues 23 Pull requests 3 Projects 1 Wiki Pulse Graphs New issue geom_errorbar does not geom_errorbar linetype work properly with position_dodge in 0.9.0 #431 Closed michaelhallquist opened this Issue Mar barplot with error bars r 8, 2012 · 6 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned
3 participants michaelhallquist commented Mar 8, 2012 A plot I previously created in ggplot2-0.8.9 no longer dodges the error bars despite properly dodging geom_line and geom_point. Here's the command I used. I'm running http://docs.ggplot2.org/current/position_dodge.html R 2.14.1 on Mac OS X 10.7. Downgrading to 0.8.9 fixed this problem. The result is a plot where the points and lines are dodged, but the error bars are are overlaid. limits <- aes(ymax=mean + std.error, ymin=mean - std.error, linetype=NULL) base_size=11 pos <- position_dodge(width=0.2) Totalplot <- ggplot(allTotal, aes(x=time, y=mean, linetype=group)) + theme_bw(base_size=base_size) + geom_line(position=pos, size=1.1) + geom_errorbar(limits, width=0.2, position=pos) + geom_point(position=pos, size=2.5) + facet_wrap(~Disorder) + ylab(paste("Mean number https://github.com/hadley/ggplot2/issues/431 of total PD symptoms")) + xlab("Time since enrollment (years)") + opts(legend.key.size=unit(2, "lines"), axis.ticks.margin = unit(0.25, "lines"), plot.margin = unit(c(1, 1, 0.5, 0.5), "lines")) + opts(panel.margin = unit(0.7, "lines")) + scale_x_continuous(breaks=c(0,1,2,3)) + scale_linetype("Latent\ntrajectory") + opts(axis.title.x = theme_text(size = base_size, vjust = 0.0)) + opts(axis.title.y = theme_text(size = base_size, hjust = 0.5, vjust=0.3, angle=90)) + opts(legend.title = theme_text(size = base_size * 0.8, hjust = 0)) Collaborator wch commented Mar 8, 2012 It's hard to verify your issue without example data... But this works for me: dat <- data.frame(x=1:2, y=1:6, g=LETTERS[1:3], e=.5) pos <- position_dodge(.2) lim <- aes(ymin=y-e, ymax=y+e) ggplot(dat, aes(x=x, y=y, colour=g)) + geom_point(position=pos) + geom_errorbar(lim, position=pos, width=.1) michaelhallquist commented Mar 9, 2012 Thanks for the follow-up. I should have included a reproducible example... When I tried to create a simple one extending the example you provided, I couldn't get the problem to show-up. But here is a dput of the actual data (which is pretty simple), along with a pared down version of the ggplot call that creates the failure to dodge error bars. My sense is that it has to do with faceting and the order of the data. Apparently this issue is more complicated than I realized, though. On 0.9.0,
Installing R/RStudio Running R/RStudio R Programming Basics Getting Help Installing R Packages R Built-in data sets Importing Data Preparing Files Importing txt|csv: R Base Functions Fast Importing txt|csv: http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization readr package Importing Excel Files Exporting Data Exporting to txt|csv Files: R Base http://stackoverflow.com/questions/13050516/how-to-make-dodge-in-geom-bar-agree-with-dodge-in-geom-errorbar-geom-point Functions Fast Exporting to txt|csv Files: readr package Exporting to Excel Files Saving Data into RDATA and RDS Formats Word Document Word Document from Template Add Table into Word Document Powerpoint Document Editable Graph From R to Powerpoint Reshaping Data Data Manipulation Data Visualization R Base Graphs Lattice Graphs Ggplot2 3D error bars Graphics How to Choose Great Colors? Basic Statistics Descriptive Statistics and Graphics Normality Test in R Statistical Tests and Assumptions Correlation Analysis Correlation Test Between Two Variables in R Correlation Matrix: Analyze, Format & Visualize Visualize Correlation Matrix using Correlogram Elegant Correlation Table using xtable R Package Correlation Matrix : An R Function to Do All You Need Comparing Means One-Sample vs Standard ggplot2 dodge error Known Mean One-Sample T-test (parametric) One-Sample Wilcoxon Test (non-parametric) Two Independent Groups Unpaired Two Samples T-test (parametric) Unpaired Two-Samples Wilcoxon Test (non-parametric) Paired Samples Paired Samples T-test (parametric) Paired Samples Wilcoxon Test (non-parametric) More Than Two Groups One-Way ANOVA Test in R Two-Way ANOVA Test in R MANOVA: Multivariate ANOVA Kruskal-Wallis (non-parametric) Comparing Variances F-Test: Compare Two Variances Compare Multiple Sample Variances Comparing Proportions One-Proportion Z-Test Two-Proportions Z-Test Chi-Square Goodness of Fit Test Chi-Square Test of Independence Cluster Analysis Overview Distance Measures Basic Clustering Partitionning Methods Hierarchical Clustering Clustering Evaluation & Validation Clustering Tendency Optimal Number of Clusters Validation Statistics Compare Clustering Algorithms p-value for Hierarchical Clustering Quick Guide for Cluster Analysis Clustering Visualization Visual Enhancement of Clustering Beautiful Dendrograms Static and Interactive Heatmap Advanced Clustering Fuzzy Clustering Model-Based Clustering Density-Based Clustering Hybrid Hierarchical Kmeans HCPC R Packages R packages developed by STHDA for easier data analyses and visualization: factoextra, survminer and ggpubr. Learn more >> Support Forum Contact R Books Download ggplot2 ebook Special Offer for You Today! 3D Plots in R R Book To Be Published Book main contents available at: Unsupervised Machine Learning Book Content Subscrib
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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point up vote 8 down vote favorite 1 I have a dataset where measurements are made for different groups at different days. I want to have side by side bars representing the measurements at the different days for the different groups with the groups of bars spaced according to day of measurement with errorbars overlaid to them. I'm having trouble with making the dodging in geom_bar agree with the dodge on geom_errorbar. Here is a simple piece of code: days = data.frame(day=c(0,1,8,15)); groups = data.frame(group=c("A","B","C","D", "E"), means=seq(0,1,length=5)); my_data = merge(days, groups); my_data$mid = exp(my_data$means+rnorm(nrow(my_data), sd=0.25)); my_data$sigma = 0.1; png(file="bar_and_errors_example.png", height=900, width=1200); plot(ggplot(my_data, aes(x=day, weight=mid, ymin=mid-sigma, ymax=mid+sigma, fill=group)) + geom_bar (position=position_dodge(width=0.5)) + geom_errorbar (position=position_dodge(width=0.5), colour="black") + geom_point (position=position_dodge(width=0.5), aes(y=mid, colour=group))); dev.off(); In the plot, the errorsegments appears with a fixed offset from its bar (sorry, no plots allowed for newbies even if ggplot2 is the subject). When binwidth is adjusted in geom_bar, the offset is not fixed and changes from day to day. Notice, that geom_errorbar and geom_point dodge in tandem. How do I get geom_bar to agree with the other two? Any help appreciated. r plot ggplot2 share|improve this question edited Oct 25 '13 at 16:26 Julius 16.7k64260 asked Oct 24 '12 at 13:40 user1771185 4112 add a comment| 2 Answers 2 active oldest votes up vote 8 down vote The alignment problems are due, in part, to