{"id":351,"date":"2019-12-05T09:32:16","date_gmt":"2019-12-05T14:32:16","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=351"},"modified":"2024-01-09T14:33:29","modified_gmt":"2024-01-09T19:33:29","slug":"making-a-publication-ready-kaplan-meier-plot-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2019\/12\/05\/making-a-publication-ready-kaplan-meier-plot-in-stata\/","title":{"rendered":"Making a publication-ready Kaplan-Meier plot in Stata"},"content":{"rendered":"\n<p><em>This post is a bit dated but is a nice primer on KM curves. For a more recent one that includes printing the HR results on the KM curve, see <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2024\/01\/09\/printing-hazard-ratio-on-kaplan-meier-curve-in-stata\/\" target=\"_blank\" rel=\"noreferrer noopener\">this post<\/a>. <\/em><\/p>\n\n\n\n<p>In the early Winter of 2019, <a href=\"https:\/\/jamanetwork.com\/journals\/jamanetworkopen\/fullarticle\/2756254\">we had a paper published in JAMA: Network Open<\/a> using the TOPCAT trial dataset looking at association between beta-blocker use at baseline and incident heart failure admissions. We obtained the data from the <a href=\"https:\/\/biolincc.nhlbi.nih.gov\/studies\/\">NIH\/NHLBI BioLINCC repository<\/a>. This is an incredible resource for datasets. With a quick application, IRB, and DUA, you can get world-class datasets from landmark clinical trials like ACCORD, SPRINT, TOPCAT, etc.. <\/p>\n\n\n\n<p>In this analysis we needed to put together a Kaplan-Meier plot for Figure 2 (sometimes called a survival plot). Well, <em>technically <\/em>it&#8217;s a cumulative incidence plot since the line starts a 0% and creeps up as events happen rather than starting at 100% and dropping down as events happen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Features of this plot that are somewhat unique:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lines are not only different colors, one is dashed and one is solid. This is key to avoiding color publication costs in some journals as it can be made greyscale and folks can still figure out which line is which. Even if there wasn&#8217;t an issue with color costs, including differing line patterns helps folks who may be colorblind. This is done with the -plot1opts- and -plot2opts- commands.  <\/li>\n\n\n\n<li>Text label for Log-Rank!<\/li>\n\n\n\n<li>Bonus example of how to use italics in text. It&#8217;s {it:YOUR TEXT}. This could also have been bold using {bf:YOUR TEXT} instead.<\/li>\n\n\n\n<li>Survival table that lines up with the years on the x axis!<\/li>\n\n\n\n<li>Pretty legend on 2 rows with custom labels. <\/li>\n<\/ul>\n\n\n\n<p>Of course, the JAMA folks remade my figure for the actual publication. Womp, womp. I still like how it came out. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What the figure looks like<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"745\" src=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/km_curve_ef50_2_bl_bb-1024x745.png\" alt=\"\" class=\"wp-image-354\" style=\"width:549px;height:398px\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/km_curve_ef50_2_bl_bb-1024x745.png 1024w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/km_curve_ef50_2_bl_bb-300x218.png 300w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/km_curve_ef50_2_bl_bb-768x559.png 768w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/km_curve_ef50_2_bl_bb-1536x1117.png 1536w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/12\/km_curve_ef50_2_bl_bb.png 2000w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Code to make this figure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ STEP 1: tell stata that it's time to event data. \n\/\/ Use the -stset- command. Syntax:\n\/\/ stset &#091;time], failure(&#091;thing that is 0 or 1 for the event of outcome]) ...\n\/\/ id(&#091;participant id, useful if doing time varying covariates]) ...\n\/\/ scale(&#091;time scale, here 365.25 days=1 year)\n\nstset days_hfhosp, f(hfhosp) id(id) scale(365.25)\n\n\/\/ STEP 2: Set the color scheme.\n\/\/ I don't like the default stata color scheme.\n\/\/ Try s1color or s1mono\n\nset scheme s1color \n\n\/\/ STEP 3: Here's the actual figure!\n\nsts graph if efstratus50==1 \/\/\/\n, \/\/\/ this was a subset of patients with EF &gt;=50%\nfail \/\/\/ this makes the line starts at the bottom of the Y axis instead of the top\nby(baselinebb0n1y) \/\/\/ this is the variable that defines the two groups, bb use at baseline or none\ntitle(\"Cumulative Heart Failure Hospitalizations\") \/\/\/ Title!\nt1title(\"Among TOPCAT Participants with EF \u226550%\") \/\/\/ subtitle!\nxtitle(\"Year of Follow-Up\") \/\/\/ x label!\nytitle(\"Cumulative Incidence\") \/\/\/ y label!\nyla(0 \"0%\" .25 \"25%\" .5 \"50%\" .75 \"75%\" 1 \"100%\", angle(0)) \/\/\/ Y-label values! Angle(0) rotates them.\nxla(0(1)6) \/\/\/ X-label values! From 0 to 6 years with 1 year intervals in between\ntext(0.63 3 \"Log-Rank {it:p}&lt;0.001&quot;, placement(e) size(medium)) \/\/\/ floating label with italics!\nlegend(order(1 &quot;No Beta-Blocker&quot; 2 &quot;Beta-Blocker&quot;) rows(2)) \/\/\/ Legend, forced on two rows\nplot1opts(lpattern(dash) lcolor(red)) \/\/\/ this forces the first line to be dashed and red\nplot2opts(lpattern(solid) lcolor(blue)) \/\/\/ this forces the second line to be solid and blue\nrisktable(0(1)6 , size(small) order(1 &quot;No Beta-Blocker&quot; 2 &quot;Beta-Blocker&quot;)) \/\/ the numbers under the X axis\n\n\/\/ STEP 4: Export the graph as a tiny PNG file for the draft and \n\/\/ tif file to upload with the manuscript submission. \ngraph export &quot;Figure 2.png&quot;, replace width(2000)\ngraph export &quot;Figure 2.tif&quot;, replace width(2000)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post is a bit dated but is a nice primer on KM curves. For a more recent one that includes printing the HR results on the KM curve, see this post. In the early Winter of 2019, we had a paper published in JAMA: Network Open using the TOPCAT trial dataset looking at association &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2019\/12\/05\/making-a-publication-ready-kaplan-meier-plot-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Making a publication-ready Kaplan-Meier plot 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-351","post","type-post","status-publish","format-standard","hentry","category-stata-code"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/351","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=351"}],"version-history":[{"count":16,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":1698,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/351\/revisions\/1698"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}