{"id":1284,"date":"2012-06-27T21:41:46","date_gmt":"2012-06-27T19:41:46","guid":{"rendered":"http:\/\/sickel.net\/blogg\/?p=1284"},"modified":"2012-07-05T21:20:37","modified_gmt":"2012-07-05T19:20:37","slug":"r-barplot-with-error-bars","status":"publish","type":"post","link":"https:\/\/sickel.net\/blogg\/?p=1284","title":{"rendered":"R &#8211; barplot with error-bars"},"content":{"rendered":"<p>I have not found any simple way to make a barplot with error-bars in R, so I had to write a script for it:<\/p>\n<pre>\r\nerrorbarplot< -function(data,error,ylab,xlab,main){\r\n  palette(c('gray75','grey50','grey25','white'))\r\n  mar=par()$mar\r\n  par(xpd=T, mar=par()$mar+c(0,0,0,8)) # Makes a bit space to place the legend\r\n  maxy=max(data+error)*1.05            # Space enough for the max error + some space\r\n  bp<-barplot(data,beside=TRUE,ylab=ylab,xlab=xlab,main=main,ylim=c(0,maxy),col=c(1,2))\r\n  segments(bp,data-error,bp,data+error) # Draws the vertical lines in the error plot   \r\n  ew=(bp[2,1]-bp[1,1])\/2                # Sets the width of the error bars as half the bare\r\n  ew=ew\/2\r\n  segments(bp-ew,data-error,bp+ew,data-error)\r\n  segments(bp-ew,data+error,bp+ew,data+error)\r\n  legend(max(bp)+1,maxy,rownames(data),fill=c(1,2))\r\n  par(mar=mar)                           # Resets the width of the plotmargins. \r\n}\r\n<\/pre>\n<p>This is called with a data set like: (It may of cource be produced other ways, just as long as you end up with one data matrix and one error-matrix)<\/p>\n<pre>\r\nrn=c('Aa','Bv','La','Pap','Sg','Vm','Ac','Ao','Af','Fr')\r\nli=c(99.9,116.1,291.8,98.5,33.7,142.9,5.4,13.8,3.5,74.9)\r\nsh=c(26.1,254.9,252,77.6,62.7,147.4,0,17.6,0.4,54.8)\r\nseli=c(9.1,13.3,73.4,10.5,5.9,40.2,1.8,5,1.7,8.7)\r\nsesh=c(7.7,84.6,54.2,4.4,11.3,17.4,0,13.7,0.2,3.8)\r\n\r\n\r\ndimnames=list(c('Something','Something else'),rn)\r\ndata=matrix(c(li,sh),nrow=2,byrow=TRUE,dimnames=dimnames)\r\nerror=matrix(c(seli,sesh),nrow=2,byrow=TRUE,dimnames=dimnames)\r\nylab=expression(paste(alpha,'something'))\r\nmain='Main title'\r\nxlab='X-label'\r\nerrorbarplot(data,error,ylab,xlab,main)\r\n<\/pre>\n<p><a href=\"http:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1285\" data-permalink=\"https:\/\/sickel.net\/blogg\/?attachment_id=1285\" data-orig-file=\"https:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot.png\" data-orig-size=\"480,480\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"errorbarplot\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot.png\" src=\"http:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot.png\" alt=\"A bar plot with errorbars - made in R\" title=\"errorbarplot\" width=\"480\" height=\"480\" class=\"alignnone size-full wp-image-1285\" srcset=\"https:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot.png 480w, https:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot-150x150.png 150w, https:\/\/sickel.net\/blogg\/wp-content\/2012\/06\/errorbarplot-300x300.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have not found any simple way to make a barplot with error-bars in R, so I had to write a script for it: errorbarplot< -function(data,error,ylab,xlab,main){ palette(c('gray75','grey50','grey25','white')) mar=par()$mar par(xpd=T, mar=par()$mar+c(0,0,0,8)) # Makes a bit space to place the legend maxy=max(data+error)*1.05 &hellip; <a href=\"https:\/\/sickel.net\/blogg\/?p=1284\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[30],"tags":[],"class_list":["post-1284","post","type-post","status-publish","format-standard","hentry","category-r"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnVtD-kI","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1284"}],"version-history":[{"count":4,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1284\/revisions"}],"predecessor-version":[{"id":1292,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1284\/revisions\/1292"}],"wp:attachment":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}