{"id":101,"date":"2016-08-02T11:53:34","date_gmt":"2016-08-02T18:53:34","guid":{"rendered":"http:\/\/blogs.oregonstate.edu\/david-rothwell\/?p=101"},"modified":"2016-10-21T10:09:20","modified_gmt":"2016-10-21T17:09:20","slug":"example-post","status":"publish","type":"post","link":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/2016\/08\/02\/example-post\/","title":{"rendered":"Grouped bar chart"},"content":{"rendered":"<p>Recently I&#8217;ve needed to plot compositional data by one or more groups. These are usually in the form of a categorical variable (ordered or not) and a binary variable to distinguish two groups; e.g., minority status or poverty (0\/1). I was struggling to plot the categorical variable across the two groups so that the bars sum to 100% for each group. Let&#8217;s start with a simple example.<\/p>\n<p>Data is from <a href=\"https:\/\/usa.ipums.org\/usa\/\">IPUMS<\/a>. My data is (here) with setup (here). We have an exhaustive five category grouping of family structure: (1) two adults no working woman, (2) two adults with working woman, (3) single woman not working, (4) single woman working, and (5) single male.<\/p>\n<p>Let&#8217;s say we want to examine poverty status across family structure. Poverty is measured using the US Census official poverty measure. We want to analyze the family structure compositions of the poor versus non-poor. <!--more--><\/p>\n<p>Let&#8217;s begin with a descriptive barchart of family structure using the <a href=\"http:\/\/fmwww.bc.edu\/RePEc\/bocode\/c\/catplot.html\">catplot package<\/a> from Nicholas J. Cox.<\/p>\n<pre class=\"pre-scrollable\">catplot fams, percent \/\/\/\r\nytitle(\"Percent\") \/\/\/\r\ntitle(\"Poverty and family structure\") \/\/\/\r\nsubtitle(\"\") \/\/\/\r\nnote(\"Source: IPUMS ACS\") \/\/\/\r\nysize(3) blabel(bar, format(%9.1f))\r\ngraph export catplot1.png, replace\r\n<\/pre>\n<p><a href=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-115 aligncenter\" src=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot1-1-300x164.png\" alt=\"catplot1\" width=\"507\" height=\"277\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2610\/files\/2016\/08\/catplot1-1-300x164.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2610\/files\/2016\/08\/catplot1-1.png 731w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/a><\/p>\n<p>Now let&#8217;s add the poverty status.<\/p>\n<p><code>catplot povstat fams, percent asyvars \/\/\/<br \/>\nytitle(\"Percent\") \/\/\/<br \/>\nlegend(label(1 \"Non-poor\") label(2 \"Poor\")) \/\/\/<br \/>\ntitle(\"Poverty and family structure\") \/\/\/<br \/>\nsubtitle(\"\") \/\/\/<br \/>\nnote(\"Source: IPUMS ACS\") \/\/\/<br \/>\nysize(3) blabel(bar, format(%9.1f))<br \/>\ngraph export catplot1.png, replace<\/code><br \/>\n<a href=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot2-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116 aligncenter\" src=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot2-1-300x164.png\" alt=\"catplot2\" width=\"503\" height=\"275\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2610\/files\/2016\/08\/catplot2-1-300x164.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2610\/files\/2016\/08\/catplot2-1.png 731w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/a> This is not bad. However, the percent are cumulative for the entire sample. We are usually looking for compositions within group so that we can compare across groups.<\/p>\n<pre class=\"pre-scrollable\">catplot povstat fams, percent(povstat)  \/\/\/\r\nytitle(\"Percent\") \/\/\/\r\nlegend(label(1 \"Non-poor\") label(2 \"Poor\")) \/\/\/\r\ntitle(\"Poverty and family structure\") \/\/\/\r\nsubtitle(\"\") \/\/\/\r\nnote(\"Source: IPUMS ACS\") \/\/\/\r\nysize(3) blabel(bar, format(%9.1f))\r\ngraph export catplot3.png, replace\r\n<\/pre>\n<p><a href=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot3-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116 aligncenter\" src=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot3-1-300x164.png\" alt=\"catplot2\" width=\"503\" height=\"275\" \/><\/a> Again, we have improvement. But, visually seeing the 0 and 1 next to each other remains burdensome on the viewer. Can we get rid of those and add some color to facilitate comparison?<br \/>\nThe option <code>asyvars<\/code> helps here.<\/p>\n<pre class=\"pre-scrollable\">catplot povstat fams, percent(povstat) asyvars \/\/\/\r\nytitle(\"Percent\") \/\/\/\r\nlegend(label(1 \"Non-poor\") label(2 \"Poor\")) \/\/\/\r\ntitle(\"Poverty and family structure\") \/\/\/\r\nsubtitle(\"\") \/\/\/\r\nnote(\"Source: IPUMS ACS\") \/\/\/\r\nysize(3) blabel(bar, format(%9.1f))\r\ngraph export catplot4.png, replace\r\n<\/pre>\n<p><a href=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot4-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-116 aligncenter\" src=\"http:\/\/blogs.oregonstate.edu\/david-rothwell\/files\/2016\/08\/catplot4-1-300x164.png\" alt=\"catplot2\" width=\"503\" height=\"275\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I&#8217;ve needed to plot compositional data by one or more groups. These are usually in the form of a categorical variable (ordered or not) and a binary variable to distinguish two groups; e.g., minority status or poverty (0\/1). I was struggling to plot the categorical variable across the two groups so that the bars&hellip; <a href=\"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/2016\/08\/02\/example-post\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":7575,"featured_media":118,"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":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[708675,1090833,555567],"class_list":["post-101","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-bar","tag-grouped","tag-stata"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2610\/files\/2016\/08\/catplot4-1.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7jXrQ-1D","_links":{"self":[{"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/users\/7575"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":20,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/posts\/101\/revisions\/129"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/media\/118"}],"wp:attachment":[{"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.blogs.oregonstate.edu\/david-rothwell\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}