{"id":353,"date":"2019-12-10T11:06:31","date_gmt":"2019-12-10T16:06:31","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=353"},"modified":"2022-04-22T12:28:53","modified_gmt":"2022-04-22T16:28:53","slug":"figure-to-show-the-distribution-of-quartiles-plus-their-median-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2019\/12\/10\/figure-to-show-the-distribution-of-quartiles-plus-their-median-in-stata\/","title":{"rendered":"Figure to show the distribution of quartiles plus their median in Stata"},"content":{"rendered":"\n<p>Buried in the supplement of a <a href=\"https:\/\/pubmed.ncbi.nlm.nih.gov\/31800067\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"recent paper (opens in a new tab)\">recent paper<\/a> is a variant of this figure that I&#8217;m rather proud of:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/bnp-figure.png\" alt=\"\" class=\"wp-image-370\" width=\"493\" height=\"173\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/bnp-figure.png 889w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/bnp-figure-300x105.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/bnp-figure-768x270.png 768w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/figure>\n\n\n\n<p>It shows the distribution of quartiles of BNP and NT proBNP at baseline on a log scale, by use of beta blockers (BB) at baseline. It also shows the midway point of the medians. It&#8217;s a nice figure that shows the increase of BNP with beta blocker administration. My colleagues jokingly called it the &#8220;Plante Plot&#8221; since I have had it included in several drafts of manuscripts, this is just the first one in which it was published. <\/p>\n\n\n\n<p>The code for it is pretty complex and follows. <strong>Steps 1-3<\/strong> pluck out the ranges of the quartiles and their midpoints for each group and saves them as a CSV file. <strong>Steps 4-8 <\/strong>render the figure. You may find it more simple to skip Steps 1-3 and manually enter the ranges of the quartiles and their medians into an Excel file and just open up that excel file in Step 4. <\/p>\n\n\n\n<p>Alternatively, I have a do file that automates this a bit. Just type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>do https:\/\/www.uvm.edu\/~tbplante\/plante%20plot%20for%20distribution%20of%20quartiles%20v1_0.do\ndistplot &#091;xaxis variable] &#091;yaxis group name]<\/code><\/pre>\n\n\n\n<p>Good luck!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Step 1: load the database\nuse \"029b1b analytic datset.dta\", clear\n\n\/\/ Step 2: You need quartiles plus the intermediate points of\n\/\/ each quartile, which is really 8-iles.\n\/\/ Step 2a: \n\/\/ You need the maximum and minimum variables to draw the bounds\n\/\/ of the bottom and top quartile\n\/\/ this is by group, here's the first group:\nsum baseline_bnp if efstratus50==1 &amp; baselinebb0n1y==0, d\nreturn list \/\/ see that r(min) and r(max) are the points needed\n\/\/ save min and max as macros for 0th bound and 0th bound\nlocal bnp8ile_nobb_0=r(min) \/\/ beginning of q1\nlocal bnp8ile_nobb_8=r(max) \/\/ end of q4\n\/\/ Step 2b: \n\/\/ now get intermediate bounds for the 8-iles\n_pctile baseline_bnp  if efstratus50==1  &amp; baselinebb0n1y==0, percentiles(12.5(12.5)87.5) \/\/ this gets bounds by 12.5iles\nreturn list \/\/ there they are!\nlocal bnp8ile_nobb_1=r(r1) \/\/ middle of q1\nlocal bnp8ile_nobb_2=r(r2) \/\/ end of q1\/beginning of q2\nlocal bnp8ile_nobb_3=r(r3) \/\/ middle of q2\nlocal bnp8ile_nobb_4=r(r4) \/\/ end of q2\/beginning of q3\nlocal bnp8ile_nobb_5=r(r5) \/\/ middle of q3\nlocal bnp8ile_nobb_6=r(r6) \/\/ end of q3\/beginning of q4\nlocal bnp8ile_nobb_7=r(r7) \/\/ middle of q4\n\/\/ now come up with a label to eventually apply to the figure\n\/\/ don't use commas in this label since we'll save this\n\/\/ output as a CSV file and commas will screw up the cell\n\/\/ structure of a CSV (C=comma)\n\/\/ step 2c: \nlocal label_bnp_nobb=\"Baseline BNP; -BB\"\n\n\/\/ now repeat for the other groups\nsum baseline_bnp if efstratus50==1 &amp; baselinebb0n1y==1, d\nreturn list\nlocal bnp8ile_bb_0=r(min)\nlocal bnp8ile_bb_8=r(max)\n_pctile baseline_bnp  if efstratus50==1  &amp; baselinebb0n1y==1, percentiles(12.5(12.5)87.5)\nreturn list\nlocal bnp8ile_bb_1=r(r1)\nlocal bnp8ile_bb_2=r(r2)\nlocal bnp8ile_bb_3=r(r3)\nlocal bnp8ile_bb_4=r(r4)\nlocal bnp8ile_bb_5=r(r5)\nlocal bnp8ile_bb_6=r(r6)\nlocal bnp8ile_bb_7=r(r7)\nlocal label_bnp_bb=\"Baseline BNP; +BB\"\n\nsum baseline_ntprobnp if efstratus50==1 &amp; baselinebb0n1y==0, d\nreturn list\nlocal ntprobnp8ile_nobb_0=r(min)\nlocal ntprobnp8ile_nobb_8=r(max)\n_pctile baseline_ntprobnp  if efstratus50==1  &amp; baselinebb0n1y==0, percentiles(12.5(12.5)87.5)\nreturn list\nlocal ntprobnp8ile_nobb_1=r(r1)\nlocal ntprobnp8ile_nobb_2=r(r2)\nlocal ntprobnp8ile_nobb_3=r(r3)\nlocal ntprobnp8ile_nobb_4=r(r4)\nlocal ntprobnp8ile_nobb_5=r(r5)\nlocal ntprobnp8ile_nobb_6=r(r6)\nlocal ntprobnp8ile_nobb_7=r(r7)\nlocal label_ntprobnp_nobb=\"Baseline NT proBNP; +BB\"\n\nsum baseline_ntprobnp if efstratus50==1 &amp; baselinebb0n1y==1, d\nreturn list\nlocal ntprobnp8ile_bb_0=r(min)\nlocal ntprobnp8ile_bb_8=r(max)\n_pctile baseline_ntprobnp  if efstratus50==1  &amp; baselinebb0n1y==1, percentiles(12.5(12.5)87.5)\nreturn list\nlocal ntprobnp8ile_bb_1=r(r1)\nlocal ntprobnp8ile_bb_2=r(r2)\nlocal ntprobnp8ile_bb_3=r(r3)\nlocal ntprobnp8ile_bb_4=r(r4)\nlocal ntprobnp8ile_bb_5=r(r5)\nlocal ntprobnp8ile_bb_6=r(r6)\nlocal ntprobnp8ile_bb_7=r(r7)\nlocal label_ntprobnp_bb=\"Baseline NT proBNP; -BB\"\n\n\n\/\/ Step 3: save this to a csv file that we'll open up right away.\n\/\/ Note: This code goes out of frame on my blog. copy and paste it \n\/\/ into a .do file and it'll all appear. \nquietly {\ncapture log close bnp \nlog using \"bnprangefigure.csv\", replace text name(bnp)\n\/\/ this is the row of headers:\nnoisily di \"row,label,eight0,eight1,eight2,eight3,eight4,eight5,eight6,eight7,eight8\" \n\/\/ row 1:\nnoisily di \"1,`label_bnp_nobb',`bnp8ile_nobb_0',`bnp8ile_nobb_1',`bnp8ile_nobb_2',`bnp8ile_nobb_3',`bnp8ile_nobb_4',`bnp8ile_nobb_5',`bnp8ile_nobb_6',`bnp8ile_nobb_7',`bnp8ile_nobb_8'\"\n\/\/ row 2\nnoisily di \"2,`label_bnp_bb',`bnp8ile_bb_0',`bnp8ile_bb_1',`bnp8ile_bb_2',`bnp8ile_bb_3',`bnp8ile_bb_4',`bnp8ile_bb_5',`bnp8ile_bb_6',`bnp8ile_bb_7',`bnp8ile_bb_8'\"\n\/\/ blank row 3:\nnoisily di \"3\"\n\/\/ row 4:\nnoisily di \"4,`label_ntprobnp_nobb',`ntprobnp8ile_nobb_0',`ntprobnp8ile_nobb_1',`ntprobnp8ile_nobb_2',`ntprobnp8ile_nobb_3',`ntprobnp8ile_nobb_4',`ntprobnp8ile_nobb_5',`ntprobnp8ile_nobb_6',`ntprobnp8ile_nobb_7',`ntprobnp8ile_nobb_8'\"\n\/\/ row 5:\nnoisily di \"5,`label_ntprobnp_bb',`ntprobnp8ile_bb_0',`ntprobnp8ile_bb_1',`ntprobnp8ile_bb_2',`ntprobnp8ile_bb_3',`ntprobnp8ile_bb_4',`ntprobnp8ile_bb_5',`ntprobnp8ile_bb_6',`ntprobnp8ile_bb_7',`ntprobnp8ile_bb_8'\"\nlog close bnp\n}\n\n\/\/ step 4: open CSV file as active database:\nimport delim using \"bnprangefigure.csv\", clear\n\/\/ note, you may opt to skip steps 1-3 and manually compile the \n\/\/ ranges of each quartile and their median into an excel file.\n\/\/ Use the -import excel- function to open that file up instead.\n\/\/ IF YOU SKIP OVER STEPS 1-3, your excel file will need the \n\/\/\/ following columns:\n\/\/ row - each group, with a blank row 3 to match the figure\n\/\/ label - title to go to the left of the figure\n\/\/ eight0 through eight8 - the even numbers are ranges of the\n\/\/    quartiles and the odd numbers are the mid-ranges.\n\/\/    See my approach in steps 2a-2b on how to get these numbers.\n\n\/\/ step 5: steal the labels. note skipping row 3 since it's blank\nlocal label1=label&#091;1]\nlocal label2=label&#091;2] \nlocal label4=label&#091;4] \/\/ NO ROW 3!!\nlocal label5=label&#091;5]\n\n\/\/ step 6: pluck the intermediate points of each quartile\n\/\/ which are 8-iles 1, 3, 5 and 7\n\/\/ and repeat for each row\nlocal bar1row1=eight1&#091;1]\nlocal bar2row1=eight3&#091;1]\nlocal bar3row1=eight5&#091;1]\nlocal bar4row1=eight7&#091;1]\n\nlocal bar1row2=eight1&#091;2]\nlocal bar2row2=eight3&#091;2]\nlocal bar3row2=eight5&#091;2]\nlocal bar4row2=eight7&#091;2]\n\n\/\/ no row 3 in this figure\n\nlocal bar1row4=eight1&#091;4]\nlocal bar2row4=eight3&#091;4]\nlocal bar3row4=eight5&#091;4]\nlocal bar4row4=eight7&#091;4]\n\nlocal bar1row5=eight1&#091;5]\nlocal bar2row5=eight3&#091;5]\nlocal bar3row5=eight5&#091;5]\nlocal bar4row5=eight7&#091;5]\n\n\/\/ step 7: pick a different scheme than the default stata one\n\/\/ I like s1mono or s1color\nset scheme s1mono\n\n\/\/ step 8: complex graph.\n\/\/ NOTE: RUN THIS SCRIPT FROM THE TOP EVERY TIME because\n\/\/ stata likes to drop the macros (\"local\" commands) and \n\/\/ the things inside of the ticks will be missing if you \n\/\/ just run starting at the \"graph twoway\" below\n\/\/ \n\/\/ step 8a: rbar the ends of the quartiles, which is:\n\/\/ 0 to 2, 2 to 4, 4 to 6, and 6 to 8\n\/\/\n\/\/ step 8b: apply the labels\n\/\/\n\/\/ step 8c: place a vertical bar at the midpoints of the\n\/\/ quartiles, which are at: 1, 3, 5, and 7. A bug in Stata\n\/\/ is that a centered label (placement(c)) is actually a smidge\n\/\/ south still, so the rows are offset by 0.13. You'll notice\n\/\/ the Y label in the text box is row value minus 0.13 (0.87, etc.)\n\/\/ to account for that.\n\/\/\n\/\/ step 8d: adjust the aspect ratio to get the bar character (\"|\") \n\/\/ to fit within the width of the the bar itself.\n\/\/\n\ngraph twoway \/\/\/ step 8a:\n(rbar eight0 eight2 row , horizontal) \/\/\/ \n(rbar eight2 eight4 row , horizontal) \/\/\/\n(rbar eight4 eight6 row , horizontal) \/\/\/\n(rbar eight6 eight8 row , horizontal) \/\/\/\n, \/\/\/\nyscale(reverse) \/\/\/\nxscale(log) \/\/\/\nt2title(\"Quartiles of BNP or NT-proBNP, EF \u226550%\", justification(center)) \/\/\/ \nxla(1 \"1\" 10 \"10\" 100 \"100\" 1000 \"1000\" 10000 \"10000\" 20000 \"20000\", angle(45)) \/\/\/ step 8b:\nyla(1 \"`label1'\" 2 \"`label2'\" 4 \"`label4'\" 5 \"`label5'\", angle(horizontal)) \/\/\/\nytitle(\" \") \/\/\/\nxtitle(\"BNP\/NTproBNP Range (Log Scale)\") \/\/\/\nlegend(off) \/\/\/\n\/\/\/ step 8c:\ntext(0.87 `bar1row1' \"|\", color(white) placement(c)) \/\/\/\ntext(0.87 `bar2row1' \"|\", color(white) placement(c)) \/\/\/\ntext(0.87 `bar3row1' \"|\", color(white) placement(c)) \/\/\/\ntext(0.87 `bar4row1' \"|\", color(white) placement(c)) \/\/\/\n\/\/\/\ntext(1.87 `bar1row2' \"|\", color(white) placement(c)) \/\/\/\ntext(1.87 `bar2row2' \"|\", color(white) placement(c)) \/\/\/\ntext(1.87 `bar3row2' \"|\", color(white) placement(c)) \/\/\/\ntext(1.87 `bar4row2' \"|\", color(white) placement(c)) \/\/\/\n\/\/\/\ntext(3.87 `bar1row4' \"|\", color(white) placement(c)) \/\/\/\ntext(3.87 `bar2row4' \"|\", color(white) placement(c)) \/\/\/\ntext(3.87 `bar3row4' \"|\", color(white) placement(c)) \/\/\/\ntext(3.87 `bar4row4' \"|\", color(white) placement(c)) \/\/\/\n\/\/\/\ntext(4.87 `bar1row5' \"|\", color(white) placement(c)) \/\/\/\ntext(4.87 `bar2row5' \"|\", color(white) placement(c)) \/\/\/\ntext(4.87 `bar3row5' \"|\", color(white) placement(c)) \/\/\/\ntext(4.87 `bar4row5' \"|\", color(white) placement(c)) \/\/\/\n\/\/\/ step 8d:\naspect(0.23)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Buried in the supplement of a recent paper is a variant of this figure that I&#8217;m rather proud of: It shows the distribution of quartiles of BNP and NT proBNP at baseline on a log scale, by use of beta blockers (BB) at baseline. It also shows the midway point of the medians. It&#8217;s a &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2019\/12\/10\/figure-to-show-the-distribution-of-quartiles-plus-their-median-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Figure to show the distribution of quartiles plus their median 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-353","post","type-post","status-publish","format-standard","hentry","category-stata-code"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/353","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=353"}],"version-history":[{"count":9,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":1009,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/353\/revisions\/1009"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}