{"id":1474,"date":"2023-07-31T15:33:06","date_gmt":"2023-07-31T19:33:06","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=1474"},"modified":"2023-07-31T15:40:11","modified_gmt":"2023-07-31T19:40:11","slug":"stata-graph-box-boxplots-with-different-colors-for-over-groups","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2023\/07\/31\/stata-graph-box-boxplots-with-different-colors-for-over-groups\/","title":{"rendered":"Stata graph box boxplots with different colors for &#8211;over&#8211; groups"},"content":{"rendered":"\n<p>I recently had to make some boxplots with Stata&#8217;s &#8211;graph box&#8211; command. I find this to be challenging to use since it varies from syntax from the &#8211;twoway&#8211; command set that I use all the time. I was using the &#8211;over&#8211; subcommand x2 and wanted to change the colors of each box &amp; dots by group from the first &#8211;over&#8211; subcommand. I found some helpful details on the Statalist Forum <a rel=\"noreferrer noopener\" href=\"https:\/\/www.statalist.org\/forums\/forum\/general-stata-discussion\/general\/1535335-change-color-of-boxplots-without-asyvars-by-keeping-xlabels\" target=\"_blank\">here<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/www.statalist.org\/forums\/forum\/general-stata-discussion\/general\/1379636-formatting-outliers-in-graph-box-change-of-color-size-only-manually-for-every-outlier\" target=\"_blank\">here<\/a>. Here&#8217;s what I did to accomplish this, using the help from the Statalist forum. <\/p>\n\n\n\n<p>Some tweaks here: I wanted to show rotate some labels 45 degrees with &#8211;angle&#8211; and I also aggressively labeled variables and their values so I didn&#8217;t need to manually relabel the figure (which is done with the &#8211;relabel&#8211; subcommand if needed). It takes an extra 30 seconds to label variables and values, and will save you lots of headbanging fiddling with the &#8211;relabel&#8211; command, so just label your variables and values from the start.<\/p>\n\n\n\n<p>This example uses fake data. Code follows the picture. Good luck!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2023\/07\/image.png\" alt=\"\" class=\"wp-image-1477\" width=\"530\" height=\"318\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2023\/07\/image.png 787w, https:\/\/blog.uvm.edu\/tbplante\/files\/2023\/07\/image-300x180.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2023\/07\/image-768x461.png 768w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>clear all\nset obs 1000 \/\/ blank dataset with 1000 observations\nset seed 8675309 \/\/ jenny seed\ngen group = round(runiform()) \/\/ make group that is 0 or 1\ngen time = round(3*runiform()) \/\/ make 4 times, 0 through 3\nreplace time = time+1 \/\/ now time is 1 through 4\ngen tommy2tone = 100*rbeta(3,10) \/\/ fake skewed data\n\n\/\/ now apply labels to variables.\n\/\/ technically you only need to label the 3rd one\n\/\/ of these since categorical variable value labels\n\/\/ are shown instead of the variable label itself,\n\/\/ but might as well do all 3 in case you need them\n\/\/ labeled somewhere else.\nlabel variable group \"Group\"\nlabel variable time \"Time\"\nlabel variable tommy2tone \"Jenny Lyrics remembered, %\"\n\n\/\/ now make value labels.\n* group\nlabel define grouplab 0 \"Tommy\" 1 \"Tutone\"\nlabel values group grouplab \/\/DON'T FORGET TO APPLY LABELS\n\n* time\nlabel define timelab 1 \"Time 1\" 2 \"Time 2\" 3 \"Time 3\" 4 \"Time 4\"\nlabel values time timelab \/\/DON'T FORGET TO APPLY LABELS\n\n\/\/ code for boxplot\ngraph box tommy2tone \/\/\/\n, \/\/\/\nover(group, label(angle(45))) \/\/\/\nover(time) \/\/\/\nscale(1.3) \/\/\/ embiggen labels &amp; figure components\nbox(1, color(red)) marker(1, mcolor(red)) \/\/\/\nbox(2, color(blue)) marker(2, mcolor(blue)) \/\/\/\nasyvars showyvars leg(off)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently had to make some boxplots with Stata&#8217;s &#8211;graph box&#8211; command. I find this to be challenging to use since it varies from syntax from the &#8211;twoway&#8211; command set that I use all the time. I was using the &#8211;over&#8211; subcommand x2 and wanted to change the colors of each box &amp; dots by &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2023\/07\/31\/stata-graph-box-boxplots-with-different-colors-for-over-groups\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Stata graph box boxplots with different colors for &#8211;over&#8211; groups<\/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":[703438,703460,703459,502556],"class_list":["post-1474","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-boxplot","tag-changing-color","tag-graph-box","tag-stata"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1474","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=1474"}],"version-history":[{"count":6,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1474\/revisions"}],"predecessor-version":[{"id":1485,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1474\/revisions\/1485"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=1474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=1474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=1474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}