{"id":196,"date":"2018-03-13T15:00:29","date_gmt":"2018-03-13T19:00:29","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=196"},"modified":"2020-09-16T09:12:06","modified_gmt":"2020-09-16T13:12:06","slug":"making-a-horizontal-stacked-bar-graph-with-graph-twoway-rbar-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2018\/03\/13\/making-a-horizontal-stacked-bar-graph-with-graph-twoway-rbar-in-stata\/","title":{"rendered":"Making a horizontal stacked bar graph with -graph twoway rbar- in Stata"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Making a horizontal stacked bar graph in Stata<\/h2>\n\n\n\n<p>I spent a bit of time making a variation of this figure today. (The data here are made up.) I&#8217;m pleased with how it came out. I first tried to use the -graph bar, horizontal- command, but it didn&#8217;t give me as much customization as -twoway graph rbar\u2026, horizontal-. I think it looks pretty slick.<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:85% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"257\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/stacked-bar-graph-stata.jpg\" alt=\"\" class=\"wp-image-197\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/stacked-bar-graph-stata.jpg 684w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/stacked-bar-graph-stata-300x113.jpg 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Start with an Excel file<\/h2>\n\n\n\n<p>I made an Excel file called\u00a0<em><strong>stacked bar graph data.xlsx\u00a0<\/strong><\/em>that I saved in the same folder as a .do file. I closed Stata and reopened that .do file from Windows explorer so that Stata set the working directory as the same folder that contains the .do file. More importantly, it set the working directory as the same folder that <em>also\u00a0<\/em>contains\u00a0the Excel file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"138\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/excel-dataset-for-stacked-bar.jpg\" alt=\"\" class=\"wp-image-198\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/excel-dataset-for-stacked-bar.jpg 449w, https:\/\/blog.uvm.edu\/tbplante\/files\/2018\/03\/excel-dataset-for-stacked-bar-300x92.jpg 300w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><\/figure>\n\n\n\n<p><em>Group<\/em> is the number of the individual bars, <em>bottom<\/em> is the bottom of the first segment of a bar, <em>q1<\/em> is the top of the first segment of each bar. The rest should be obvious. I made this for quartiles, hence the q1-4 names. You can tweak the numbers by editing the Excel file, hitting save\/close, and rerunning the .do file.<\/p>\n\n\n\n<p><em><strong>Make sure that you save and close Excel before running the .do file or the .do file won&#8217;t run and you will be sad.\u00a0<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">My .do file for making this horizontal stacked bar graph<\/h2>\n\n\n\n<p>Here&#8217;s my code! I hope it&#8217;s useful.<\/p>\n\n\n<pre>********************************************************************************\n******************************IMPORT DATA HERE**********************************\n********************************************************************************\nimport excel \"stacked bar graph data.xlsx\", firstrow clear \/\/ make sure that excel\n\/\/                                                           is closed before you\n\/\/                                                           run this script!\ndestring, replace\n\n********************************************************************************\n******************************CODE STARTS HERE**********************************\n********************************************************************************\ncapture ssc install scheme-burd, replace \/\/ this installs nicer color schemes\n\/\/ see the schemes here: https:\/\/github.com\/briatte\/burd\nset scheme burd4 \n\ngraph twoway \/\/\/\n(rbar bottom q1 group, horizontal) \/\/\/\n(rbar q1 q2 group, horizontal) \/\/\/\n(rbar q2 q3 group, horizontal) \/\/\/\n(rbar q3 q4 group, horizontal) \/\/\/\n, \/\/\/ if you modify this file and it stops working, check the placement of this comma\nxscale(log) \/\/\/ make the x axis log scale\nxla(0.25 \"0.25\" 0.5 \"0.5\" 1 \"1\" 2.5 \"2.5\" 5 \"5\" 10 \"10\" 25 \"25\" 50 \"50\" 100 \"100\") \/\/\/\nyla(1 \"Group 1\" 2 \"Group 2\" 3 \"Group 3\" 4 \"Group 4\") \/\/\/\nytitle(\"Y title\") \/\/\/\nxtitle(\"X title\") \/\/\/\ntext(4 1.2 \"Q1\", color(white)) \/\/\/ first number is y second is x.\ntext(4 4.3 \"Q2\", color(white)) \/\/\/\ntext(4 20 \"Q3\", color(white)) \/\/\/\ntext(4 90 \"Q4\", color(white)) \/\/\/\nlegend(off) \/\/\/ no legend, aspect is the shape of the figure. 1 is tall and thin.\naspect(.25) \n\/\/\n\/\/ export the graph as a PNG file\ngraph export \"stacked graph.png\", replace width(2000)\n\/\/ graph export \"stacked graph.tif\", replace width(2000) \/\/ in case you want as a tiff\n\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Making a horizontal stacked bar graph in Stata I spent a bit of time making a variation of this figure today. (The data here are made up.) I&#8217;m pleased with how it came out. I first tried to use the -graph bar, horizontal- command, but it didn&#8217;t give me as much customization as -twoway graph &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2018\/03\/13\/making-a-horizontal-stacked-bar-graph-with-graph-twoway-rbar-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Making a horizontal stacked bar graph with -graph twoway rbar- in Stata<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[477491],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-stata-code"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/users\/4473"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":9,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":562,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/196\/revisions\/562"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}