Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Swimmer Plot

    Can someone please show me how to draw a Swimmer plot in Stata ? This is a type a graph used in Oncology to present duration of response to treatment. Thank you.

  • #2
    can you show an example (following the FAQ advice to show as png) or provide a citation that includes such a graph?

    Comment


    • #3
      They look like this:

      I have seen but not used them; there is an article on how to construct them in R https://rstudio-pubs-static.s3.amazo...5a6dcddef.html.

      ​​​​​​​Martyn

      Comment


      • #4

        The workhorse of Stata graphics is the twoway command. You can reproduce virtually any graph using it. I import the data from Martyn's link and reproduce this so called "swimmer plot" (not visible to me from Martyn's post, but uploaded below). I do not bother with the legend as formatting this is straightforward.


        "Swimmer plot" from https://rstudio-pubs-static.s3.amazo...5a6dcddef.html
        Click image for larger version

Name:	download.png
Views:	1
Size:	49.8 KB
ID:	1556078


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float subjectID str15 stage float(startTime endTime) str29(isContinued responseType) float(responseStartTime responseEndTime Durable)
         1 "Stage 1" 0 18.5 "FilledArrow" "Complete response"  6.5 13.5 -.25
         2 "Stage 2" 0   17 ""            "Complete response" 10.5   17 -.25
         3 "Stage 3" 0   14 "FilledArrow" "Partial response"   2.5  3.5 -.25
         3 ""        0   14 "FilledArrow" "Partial response"     6    . -.25
         4 "Stage 4" 0 13.5 "FilledArrow" "Partial response"     7   11    .
         4 ""        0 13.5 "FilledArrow" "Partial response"  11.5    .    .
         5 "Stage 1" 0 12.5 "FilledArrow" "Complete response"  3.5  4.5 -.25
         5 ""        0 12.5 "FilledArrow" "Complete response"  6.5  8.5 -.25
         5 ""        0 12.5 "FilledArrow" "Partial response"  10.5    . -.25
         6 "Stage 2" 0 12.6 "FilledArrow" "Partial response"   2.5    7    .
         6 ""        0 12.6 "FilledArrow" "Partial response"   9.5    .    .
         7 "Stage 3" 0 11.5 ""            "Complete response"  4.5 11.5 -.25
         8 "Stage 1" 0  9.5 ""            "Complete response"    1  9.5 -.25
         9 "Stage 4" 0  8.3 ""            "Partial response"     6    8.3    .
        10 "Stage 2" 0  4.2 "FilledArrow" "Complete response"  1.2    .    .
        end
        
        gen id= subjectID*(-1.2)
        gen cont= endTime+1 if !missing(isCon)
        tw (bar endTime id if regexm(stage, "1$"), hor bcolor(blue%30)) ///
        (bar endTime id if regexm(stage, "2$"), hor bcolor(red%30)) ///
        (bar endTime id if regexm(stage, "3$"), hor bcolor(green%30)) ///
        (bar endTime id if regexm(stage, "4$"), hor bcolor(brown%30)) ///
        (scatter id responseEndTime, mcolor(black)) ///
        (scatter id responseStartTime if regexm(responseType, "^C") , ///
        sym(triangle) mcolor(red) ) (scatter id responseStartTime if ///
        regexm(responseType, "^P") , sym(triangle) mcolor(blue))  ///
        (pcarrow id endTime id cont, mcolor(black) barbsize(2) lcolor(black)) ///
        (scatter id Durable, sym(square) mcolor(black) ylab("", noticks) xsc(r(-1,.)) ///
        xlab(0/20) scheme(s1color) xtitle("Months") ytitle("Subjects Received Study Drug") ///
        leg(off) note("Each bar represents one subject in the study." ///
        "A Durable Responder is a subject who has confirmed response for at least 183 days (6 months)"))

        Res.:
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.0 KB
ID:	1556093

        Last edited by Andrew Musau; 30 May 2020, 09:20.

        Comment


        • #5
          Thank you so much.

          Comment


          • #6
            Hi Andrew - I've found your code for the swimmer's plot very helpful (thank you). If the endTime were not sorted already (as in your data input and example) - what would you add to the code to sort by endTime? I have been able to generate a swimmer's plot for my data but just having trouble sorting it out by endTime...
            Thanks in advance

            Comment


            • #7
              As the end time is constant within subject id, you can use egen's -group()- function. The end times will be sorted in ascending order, so if you are replicating #4, remove the minus sign in the first line of the code.

              Code:
              gen id= newid*(1.2)
              Here is an example:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float subjectID str15 stage float(startTime endTime)
               7 "Stage 1" 0 18.5
               5 "Stage 2" 0   17
               1 "Stage 3" 0   14
               1 ""        0   14
              10 "Stage 4" 0 13.5
              10 ""        0 13.5
               2 "Stage 1" 0 12.5
               2 ""        0 12.5
               2 ""        0 12.5
               3 "Stage 2" 0 12.6
               3 ""        0 12.6
               4 "Stage 3" 0 11.5
               6 "Stage 1" 0  9.5
               8 "Stage 4" 0  8.3
               9 "Stage 2" 0  4.2
              end
              
              egen newid= group(endTime subjectID)
              Res.:

              Code:
              . l, sep(0)
              
                   +-------------------------------------------------+
                   | subjec~D     stage   startT~e   endTime   newid |
                   |-------------------------------------------------|
                1. |        7   Stage 1          0      18.5      10 |
                2. |        5   Stage 2          0        17       9 |
                3. |        1   Stage 3          0        14       8 |
                4. |        1                    0        14       8 |
                5. |       10   Stage 4          0      13.5       7 |
                6. |       10                    0      13.5       7 |
                7. |        2   Stage 1          0      12.5       5 |
                8. |        2                    0      12.5       5 |
                9. |        2                    0      12.5       5 |
               10. |        3   Stage 2          0      12.6       6 |
               11. |        3                    0      12.6       6 |
               12. |        4   Stage 3          0      11.5       4 |
               13. |        6   Stage 1          0       9.5       3 |
               14. |        8   Stage 4          0       8.3       2 |
               15. |        9   Stage 2          0       4.2       1 |
                   +-------------------------------------------------+

              If this does not help, present a data example using the dataex command (see FAQ Advice #12 for details).

              Comment

              Working...
              X