{"id":532,"date":"2015-08-01T12:46:14","date_gmt":"2015-08-01T11:46:14","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=532"},"modified":"2025-07-01T08:44:56","modified_gmt":"2025-07-01T07:44:56","slug":"bar-chart","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/bar-chart\/","title":{"rendered":"Bar Chart"},"content":{"rendered":"\n<p>Bar charts are used to present ordinal, nominal or discrete numeral data. They are usually drawn so that the bars are vertical, but can also be drawn horizontally to aid readability. The bars (or bins) have a space between each other and don&#8217;t touch to indicate the data are not continuous (as opposed to <a href=\"https:\/\/pcool.dyndns.org\/index.php\/histogram\/\" data-type=\"page\" data-id=\"538\">histograms<\/a>, where the bins touch each other). Error bars are not required for this type of plot. A bar chart can however be stacked or grouped to identify different (sub)-groups.<\/p>\n\n\n\n<p>Download and open the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/plotbar.rda\" target=\"_blank\" rel=\"noreferrer noopener\">plotbar.rda<\/a> dataset for this example. This data set gives the number of patients admitted to an orthopaedic department that had a fracture over a ten year period (fictional data).<\/p>\n\n\n\n<p>Plot builder can be used to create this plot, but here the instructions are typed into the console. To show the data:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f80505\" class=\"has-inline-color\">plotbar\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2c05f7\" class=\"has-inline-color\">   year fractures\n1  2004      1273\n2  2005      1289\n3  2006      1325\n4  2007      1497\n5  2008      1575\n6  2009      1658\n7  2010      1767\n8  2011      1895\n9  2012      1984\n10 2013      2055\n11 2014      2146<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>Or to show each variable separately:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>plotbar$year<\/em><\/span>\n<span style=\"color: #0000ff;\"><em>&nbsp;&#91;1] 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>plotbar$fractures<\/em><\/span>\n<span style=\"color: #0000ff;\"><em>&nbsp;&#91;1] 1273 1289 1325 1497 1575 1658 1767 1895 1984 2055 2146<\/em><\/span><\/code><\/pre>\n\n\n\n<p>Use the ggplot() function to plot the bar chart. The easiest way to do this is by giving the plot a name and subsequently add instructions to it:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">bar &lt;- ggplot(<em><span style=\"color: #ff0000;\">data=plotbar, aes(x=year,y=fractures)<\/span><\/em>) + geom_bar(stat='unique')<\/span><\/em> \n<em><span style=\"color: #ff0000;\">bar<\/span><\/em><\/code><\/pre>\n\n\n\n<p>will show the plot.<\/p>\n\n\n\n<p>To add a black and white theme add the theme to bar and show it again:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">bar &lt;- bar + <\/span><\/em><em><span style=\"color: #ff0000;\">theme_bw()<\/span><\/em>\n<em><span style=\"color: #ff0000;\">bar<\/span><\/em><\/code><\/pre>\n\n\n\n<p>To add a title:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">bar &lt;- bar + ggtitle(label='Fracture Admissions')<\/span><\/em>\n<em><span style=\"color: #ff0000;\">bar<\/span><\/em><\/code><\/pre>\n\n\n\n<p>To set the x axis with yearly labels as defined in plotbar$year:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>bar &lt;- bar + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_x_continuous(name='Year', breaks=plotbar$year)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>bar<\/em><\/span><\/code><\/pre>\n\n\n\n<p>Similarly, the y axis can be set:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">bar &lt;- bar +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_y_continuous(name='Number of Fractures')<\/span><\/em>\n<em><span style=\"color: #ff0000;\">bar<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"799\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotbar-1024x799.png\" alt=\"\" class=\"wp-image-3383\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotbar-1024x799.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotbar-300x234.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotbar-768x599.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotbar-1536x1198.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotbar.png 1884w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For the creation of more complex <em>stacked bar charts<\/em> and <em>faceted bar charts<\/em>, please refer to <a href=\"https:\/\/pcool.dyndns.org\/index.php\/advanced-concepts\/\" data-type=\"page\" data-id=\"1689\">Advanced Concepts<\/a>.<\/p>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bar charts are used to present ordinal, nominal or discrete numeral data. They are usually drawn so that the bars are vertical, but can also be drawn horizontally to aid readability. The bars (or bins) have a space between each other and don&#8217;t touch to indicate the data are not continuous (as opposed to histograms, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-532","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":2,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/532\/revisions"}],"predecessor-version":[{"id":4648,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/532\/revisions\/4648"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}