{"id":1203,"date":"2022-09-26T12:21:34","date_gmt":"2022-09-26T16:21:34","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=1203"},"modified":"2022-09-26T12:35:36","modified_gmt":"2022-09-26T16:35:36","slug":"generate-random-data-make-scatterplot-with-fitted-line-and-merge-multiple-figures-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2022\/09\/26\/generate-random-data-make-scatterplot-with-fitted-line-and-merge-multiple-figures-in-stata\/","title":{"rendered":"Generate random data, make scatterplot with fitted line, and merge multiple figures in Stata"},"content":{"rendered":"\n<p>I recently made these two figures with a Stata do file that (A) generates some fake data, (B) plots the data overall with a fitted line, (C) plots each participant individually with fitted line, and (D) merges these four individual plots into one overall plot. One tricky piece of this was to get the &#8211;graph combine&#8211; command to get the four figures to be square, I had to fiddle with the &#8211;ysize&#8211; to get them to be square. I had originally tried using the &#8211;aspect&#8211; option, but it builds in lots of white space to between the figures. this way seemed to work. <\/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\/2022\/09\/figure1av2.png\" alt=\"\" class=\"wp-image-1204\" width=\"384\" height=\"384\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1av2.png 1000w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1av2-300x300.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1av2-150x150.png 150w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1av2-768x768.png 768w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/figure>\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\/2022\/09\/figure1bv2-954x1024.png\" alt=\"\" class=\"wp-image-1205\" width=\"445\" height=\"477\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1bv2-954x1024.png 954w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1bv2-280x300.png 280w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1bv2-768x824.png 768w, https:\/\/blog.uvm.edu\/tbplante\/files\/2022\/09\/figure1bv2.png 1000w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/figure>\n\n\n\n<p>Here&#8217;s the Stata code to make the above figures. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>************************\n*****clear data\/graphs**\n****set graph schemes***\n************************\nclear\ngraph drop _all\nset scheme s1mono\n\n************************\n*****generate data******\n************************\nset seed 8675309\n\/\/ generate 40 random values\nset obs 40\n\/\/...called x and y that range from - to +9\r\ngen y = runiform(-9,9)\r\ngen x = runiform(-9,9)\n\/\/ now make an indicator for each grouping of 10\r\ngen n =.\r\nreplace n= 1 if _n&gt;=0 &amp; _n&lt;=10<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>replace n= 2 if _n&gt;10 &amp; _n&lt;=20<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>replace n= 3 if _n&gt;20 &amp; _n&lt;=30<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>replace n= 4 if _n&gt;30 &amp; _n&lt;=40<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ now for each successive 10 rows, add 100, 120, 140, and 160\nreplace y = y+100 if n==1\nreplace x = x+100 if n==1\n\nreplace y = y+120 if n==2\nreplace x= x+120 if n==2\n\nreplace y= y+140 if n==3\nreplace x= x+140 if n==3\n\nreplace y= y+160 if n==4\nreplace x= x+160 if n==4<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\r************************\r\n*****single figure******\r\n************************\r\ntwoway \/\/\/\r\n\/\/\/ line of unity (45 degree line): \r\n(function y = x, ra(90 170) clpat(solid) clwidth(medium) clcolor(black)) \/\/\/\r\n\/\/\/ line of fit: \r\n(lfit y x, lcolor(red) lpattern(dash) lwidth(thick)) \/\/\/\r\n\/\/\/ dots for each group, using colors from colorbrewer2\r\n(scatter y x if n==1, mcolor(\"230 97 1\") msymbol(O) mlcolor(black)) \/\/\/\r\n(scatter y x if n==2, mcolor(\"253 184 99\") msymbol(O) mlcolor(black)) \/\/\/\r\n(scatter y x if n==3, mcolor(\"178 171 210\") msymbol(O) mlcolor(black)) \/\/\/\r\n(scatter y x if n==4, mcolor(\"94 60 153\") msymbol(O) mlcolor(black)) \/\/\/\r\n, \/\/\/\r\n\/\/\/ force size:\r\nysize(4) xsize(4) \/\/\/\r\n\/\/\/ axis titles and rows:\r\nxtitle(\"Reference BP, mm Hg\") \/\/\/\r\nytitle(\"Cuffless BP, mm Hg\") \/\/\/\r\nxla(90(20)170) \/\/\/\r\nyla(90(20)170) \/\/\/\r\nlegend(off) \/\/\/\r\n\/\/\/ labels:\r\ntext(102 109 \"\u2190Participant A\", placement(e)) \/\/\/\r\ntext(119 127 \"\u2190Participant B\", placement(e)) \/\/\/\r\ntext(142 135 \"Participant C\u2192\", placement(w)) \/\/\/\r\ntext(160 154 \"Participant D\u2192\", placement(w)) \r\n\/\/\/ export figure:\r\ngraph export figure1av2.png, replace width(1000)\r\n\r\n\r\n************************\r\n***four-piece figures***\r\n*****to merge***********\r\n************************\r\ntwoway \/\/\/\r\n(function y = x if n==1, ra(90 110) clpat(solid) clwidth(medium) clcolor(black)) \/\/\/\r\n(lfit y x if n==1, lcolor(red) lpattern(dash) lwidth(thick)) \/\/\/\r\n(scatter y x if n==1, mcolor(\"230 97 1\") msymbol(O) mlcolor(black)) \/\/\/\r\n, \/\/\/\r\ntitle(\"Participant A\") \/\/\/\r\nxtitle(\" \") \/\/\/\r\nytitle(\" \") \/\/\/\r\nyla(90(5)110) \/\/\/\r\nxla(90(5)110) \/\/\/\r\nlegend(off) \/\/\/\r\nname(figure1b1)\r\n\r\ntwoway \/\/\/\r\n(function y = x if n==2, ra(110 130) clpat(solid) clwidth(medium) clcolor(black)) \/\/\/\r\n(lfit y x if n==2, lcolor(red) lpattern(dash) lwidth(thick)) \/\/\/\r\n(scatter y x if n==2, mcolor(\"253 184 99\") msymbol(O) mlcolor(black)) \/\/\/\r\n, \/\/\/\r\ntitle(\"Participant B\") \/\/\/\r\nxtitle(\" \") \/\/\/\r\nytitle(\" \") \/\/\/\r\nyla(110(5)130) \/\/\/\r\nxla(110(5)130) \/\/\/\r\nlegend(off) \/\/\/\r\nname(figure1b2)\r\n\r\ntwoway \/\/\/\r\n(function y = x if n==3, ra(130 150) clpat(solid) clwidth(medium) clcolor(black)) \/\/\/\r\n(lfit y x if n==3, lcolor(red) lpattern(dash) lwidth(thick)) \/\/\/\r\n(scatter y x if n==3, mcolor(\"178 171 210\") msymbol(O) mlcolor(black)) \/\/\/\r\n, \/\/\/\r\ntitle(\"Participant C\") \/\/\/\r\nxtitle(\" \") \/\/\/\r\nytitle(\" \") \/\/\/\r\nyla(130(5)150) \/\/\/\r\nxla(130(5)150) \/\/\/\r\nlegend(off) \/\/\/\r\nname(figure1b3)\r\n\r\ntwoway \/\/\/\r\n(function y = x if n==4, ra(150 170) clpat(solid) clwidth(medium) clcolor(black)) \/\/\/\r\n(lfit y x if n==4, lcolor(red) lpattern(dash) lwidth(thick)) \/\/\/\r\n(scatter y x if n==4, mcolor(\"94 60 153\") msymbol(O) mlcolor(black)) \/\/\/\r\n, \/\/\/\r\ntitle(\"Participant D\") \/\/\/\r\nxtitle(\" \") \/\/\/\r\nytitle(\" \") \/\/\/\r\nyla(150(5)170) \/\/\/\r\nxla(150(5)170) \/\/\/\r\nlegend(off) \/\/\/\r\nname(figure1b4)\r\n\r\n***********************\r\n***combine figures*****\r\n***********************\r\ngraph combine figure1b1 figure1b2 figure1b3 figure1b4 \/*figure1b5*\/, \/\/\/\r\nb1title(\"Reference BP, mm Hg\") \/\/\/\r\nl1title(\"Cuffless BP, mm Hg\") \/\/\/\r\n\/\/\/ this forces the four scatterplots to be the same height\/width\r\n\/\/\/ details here:\r\n\/\/\/ https:\/\/www.stata.com\/statalist\/archive\/2013-07\/msg00842.html\r\ncols(2) iscale(.7273) ysize(5.9) graphregion(margin(zero))\r\n\r\n\/\/\/ export to figure\r\ngraph export figure1bv2.png, replace width(1000)\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently made these two figures with a Stata do file that (A) generates some fake data, (B) plots the data overall with a fitted line, (C) plots each participant individually with fitted line, and (D) merges these four individual plots into one overall plot. One tricky piece of this was to get the &#8211;graph &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2022\/09\/26\/generate-random-data-make-scatterplot-with-fitted-line-and-merge-multiple-figures-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generate random data, make scatterplot with fitted line, and merge multiple figures 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":[703409,703407,703408,703406],"class_list":["post-1203","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-graph-combine","tag-lfit","tag-random-data-generation","tag-twoway-scatter"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1203","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=1203"}],"version-history":[{"count":11,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1203\/revisions"}],"predecessor-version":[{"id":1219,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/1203\/revisions\/1219"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=1203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=1203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=1203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}