R Dotchart Error Bars
Contents |
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 bars would r dot plot with confidence intervals be more appropriate and convey the required information in a more engaging plot error bars in r manner. It wasn't as straight forward as I thought it should be to make such plots comparing the dot plot with error bars in r 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 order, which is fine ggplot error bars 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 weren't so close to the
Plotci
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 comments by email. Notify me of new posts by email. Always
by over 573 bloggers. There are many ways to follow us - By e-mail: On Facebook: If you are an R blogger yourself you are
Dot Plot Ggplot2
invited to add your own R content feed to this site (Non-English R bloggers geom_dotplot should add themselves- here) Jobs for R-usersStatistical Analyst @ Rostock, Mecklenburg-Vorpommern, GermanyData EngineerData Scientist – Post-Graduate Programme @ Nottingham, EnglandDirector, pch r Real World Informatics & Analytics Data Science @ Northbrook, Illinois, U.S.Junior statistician/demographer for UNICEF Popular Searches web scraping heatmap twitter maps time series animation boxplot shiny hadoop ggplot2 how to import image file to http://garthtarr.com/dotchart-with-error-bars-in-r/ R trading finance latex eclipse rstudio excel SQL ggplot quantmod knitr googlevis PCA market research rattle regression map tutorial coplot rcmdr Recent Posts Election 2016: Tracking Emotions with R and Python Data science for executives and managers The Worlds Economic Data, Shiny Apps and all you want to know about Propensity Score Matching! August Package Picks Slack all the things! Warsaw R-Ladies Notes from the Kölner R meeting, https://www.r-bloggers.com/r-tutorial-add-confidence-intervals-to-dotchart/ 14 October 2016 anytime 0.0.4: New features and fixes 2016-13 ‘DOM’ Version 0.3 Building a package automatically The new R Graph Gallery Network Analysis Part 3 Exercises Annotated Facets with ggplot2 Paper published: mlr - Machine Learning in R a grim knight [cont’d] 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 so I didn't want to use boxplots or other density types of visualization methods. I wanted a simple mean and 95% (~ roughly 2 standard deviations) confidence around the mean. My method of choice was to use the dotchart function. Yet that function is limited to showing the data points and not the dispersion of the data. So I needed to layer in the confidence intervals. The gre
Action (2nd ed) significantly expands upon this http://www.statmethods.net/graphs/dot.html material. Use promo code ria38 for a 38% discount. Top Menu Home The R Interface Data Input Data Management Basic Statistics Advanced Statistics https://plot.ly/r/error-bars/ Basic Graphs Advanced Graphs Blog Dot Plots Create dotplots with the dotchart(x, labels=) function, where x is a numeric vector and labels is error bars a vector of labels for each point. You can add a groups= option to designate a factor specifying how the elements of x are grouped. If so, the option gcolor= controls the color of the groups label. cex controls the size of the labels. # Simple dot plot with Dotplot
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,
main="Gas Milage for Car Models",
xlab="Miles Per Gallon") click to view # Dotplot: Grouped Sorted and Colored
# Sort by mpg, group and color by cylinder
x <- mtcars[order(mtcars$mpg),] # sort by mpg
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
main="Gas Milage for Car Models\ngrouped by cylinder",
xlab="Miles Per Gallon", gcolor="black", color=x$color) click to view Going Further Advanced dotplots can be created with the dotplot2( ) function in the Hmisc package and with the panel.dotplot( ) function in the lattice package. For many good ideas, see William Jacoby's articles on dotplots. Copyright © 2014 Robert I. Kabacoff, Ph.D. | SitemapDesigned by WebTemplateOcean.com
instant chat support from our awesome engineering team. plotly Pricing PLOTCON NYC API Sign In SIGN UP + NEW PROJECT UPGRADE REQUEST DEMO Feed Pricing Make a Chart API Sign In SIGN UP + NEW PROJECT UPGRADE REQUEST DEMO Show Sidebar Hide Sidebar Help API Libraries R Error Bars Fork on Github Navigation Back to R Error Bars in R How to add error bars to scatter plots in R. R matplotlib Python plotly.js Pandas node.js MATLAB Error Bars library(dplyr) ## Warning: package 'dplyr' was built under R version 3.2.5 library(plotly) ## Warning: package 'ggplot2' was built under R version 3.2.4 data <- mpg %>% group_by(class) %>% summarise(highway = mean(hwy), sd = 1.96 * sd(hwy), city = mean(cty)) %>%