Announcement

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

  • Boxplot with whiskers go to actual min and max instead of 1.5 IQR

    Hello, I am using Stata 16.1 (MP). I would like to create a boxplot but with whiskers going to actual min and max instead of 1.5 IQR. I was wondering if I could do that and how. Thanks!

    This is the graph I have for now. In the graph, you can see that all three variables' lower whiskers go to 1.5 IQR instead of the actual min: 2, 1.6, and 1.8. I was wondering if Stata could make the lower whiskers reach the actual min.
    Screen Shot 2023-04-07 at 2.09.50 PM.png
    Here is the example data.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte ID double(var1 var2 var3)
     1  3.142857074737549                  3             3.0625
     2                  3  2.444444417953491             2.6875
     3  4.285714149475098                  4              4.125
     4 4.5714287757873535                  . 4.5714287757873535
     5  4.285714149475098  3.444444417953491             3.8125
     6 3.2857143878936768  3.444444417953491              3.375
     7 4.5714287757873535  4.666666507720947              4.625
     8                  2 1.5555555820465088               1.75
     9  4.285714149475098  3.222222328186035             3.6875
    10 4.5714287757873535                  4               4.25
    11  4.142857074737549  3.222222328186035              3.625
    12 4.4285712242126465 3.6666667461395264                  4
    13 4.4285712242126465  3.555555582046509             3.9375
    14  4.285714149475098  3.444444417953491             3.8125
    15  4.142857074737549 3.6666667461395264              3.875
    16  4.285714149475098  4.111111164093018             4.1875
    17  4.142857074737549                  4             4.0625
    18 4.5714287757873535 3.8888888359069824             4.1875
    19  4.285714149475098  4.111111164093018             4.1875
    20 4.4285712242126465  3.555555582046509             3.9375
    21  3.142857074737549 2.8888888359069824                  3
    end

  • #2
    One way to get this is through stripplot from SSC.

    That said, while I tend to avoid the whiskers to at most 1.5 IQR rule, there is much scope here, and almost everywhere, to show informative and possibly important detail about the data as well as a minimal box plot. It is surprisingly common to find that researchers overlook the elementary but fundamental point that if half the values lie inside each box, then the other half lies outside and may need very detailed attention.

    Here var1 is a case in point, as the short upper tail deserves a detailed scrutiny that no box plot will give it.


    Further, the unnecessary legend can be removed in favour of more direct labelling.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte ID double(var1 var2 var3)
     1  3.142857074737549                  3             3.0625
     2                  3  2.444444417953491             2.6875
     3  4.285714149475098                  4              4.125
     4 4.5714287757873535                  . 4.5714287757873535
     5  4.285714149475098  3.444444417953491             3.8125
     6 3.2857143878936768  3.444444417953491              3.375
     7 4.5714287757873535  4.666666507720947              4.625
     8                  2 1.5555555820465088               1.75
     9  4.285714149475098  3.222222328186035             3.6875
    10 4.5714287757873535                  4               4.25
    11  4.142857074737549  3.222222328186035              3.625
    12 4.4285712242126465 3.6666667461395264                  4
    13 4.4285712242126465  3.555555582046509             3.9375
    14  4.285714149475098  3.444444417953491             3.8125
    15  4.142857074737549 3.6666667461395264              3.875
    16  4.285714149475098  4.111111164093018             4.1875
    17  4.142857074737549                  4             4.0625
    18 4.5714287757873535 3.8888888359069824             4.1875
    19  4.285714149475098  4.111111164093018             4.1875
    20 4.4285712242126465  3.555555582046509             3.9375
    21  3.142857074737549 2.8888888359069824                  3
    end
    
    set scheme s1color 
    
    local what ytitle(some better text should go here -- or nothing)
    
    stripplot var* , yla(, ang(h)) xla(, noticks) ms(none) box pctile(0) vertical `what' name(G1, replace)
    
    stripplot var* , yla(, ang(h)) xla(, noticks) cumul height(0.6) box(barw(0.1)) boffset(-0.1) pctile(0) vertical `what' name(G2, replace)
    Click image for larger version

Name:	minimal1.png
Views:	1
Size:	16.2 KB
ID:	1708954
    Click image for larger version

Name:	minimal2.png
Views:	1
Size:	28.9 KB
ID:	1708955


    Comment


    • #3
      See also the concurrent thread https://www.statalist.org/forums/for...-plot-in-stata

      Comment


      • #4
        Originally posted by Nick Cox View Post
        One way to get this is through stripplot from SSC.

        That said, while I tend to avoid the whiskers to at most 1.5 IQR rule, there is much scope here, and almost everywhere, to show informative and possibly important detail about the data as well as a minimal box plot. It is surprisingly common to find that researchers overlook the elementary but fundamental point that if half the values lie inside each box, then the other half lies outside and may need very detailed attention.

        Here var1 is a case in point, as the short upper tail deserves a detailed scrutiny that no box plot will give it.


        Further, the unnecessary legend can be removed in favour of more direct labelling.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte ID double(var1 var2 var3)
        1 3.142857074737549 3 3.0625
        2 3 2.444444417953491 2.6875
        3 4.285714149475098 4 4.125
        4 4.5714287757873535 . 4.5714287757873535
        5 4.285714149475098 3.444444417953491 3.8125
        6 3.2857143878936768 3.444444417953491 3.375
        7 4.5714287757873535 4.666666507720947 4.625
        8 2 1.5555555820465088 1.75
        9 4.285714149475098 3.222222328186035 3.6875
        10 4.5714287757873535 4 4.25
        11 4.142857074737549 3.222222328186035 3.625
        12 4.4285712242126465 3.6666667461395264 4
        13 4.4285712242126465 3.555555582046509 3.9375
        14 4.285714149475098 3.444444417953491 3.8125
        15 4.142857074737549 3.6666667461395264 3.875
        16 4.285714149475098 4.111111164093018 4.1875
        17 4.142857074737549 4 4.0625
        18 4.5714287757873535 3.8888888359069824 4.1875
        19 4.285714149475098 4.111111164093018 4.1875
        20 4.4285712242126465 3.555555582046509 3.9375
        21 3.142857074737549 2.8888888359069824 3
        end
        
        set scheme s1color
        
        local what ytitle(some better text should go here -- or nothing)
        
        stripplot var* , yla(, ang(h)) xla(, noticks) ms(none) box pctile(0) vertical `what' name(G1, replace)
        
        stripplot var* , yla(, ang(h)) xla(, noticks) cumul height(0.6) box(barw(0.1)) boffset(-0.1) pctile(0) vertical `what' name(G2, replace)
        [ATTACH=CONFIG]n1708954[/ATTACH] [ATTACH=CONFIG]n1708955[/ATTACH]
        Dear Nick: thank you so much! Could I ask a follow-up question? Is it possible to add caps at the end of whiskers, like the graph box produced by Stata as follows:
        Screen Shot 2023-04-07 at 2.09.50 PM.png

        Comment


        • #5
          Add the option to my code

          Code:
          whiskers(recast(rcap))
          and to change the cap length add

          Code:
          whiskers(recast(rcap) msize(*2))
          or whatever.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Add the option to my code

            Code:
            whiskers(recast(rcap))
            and to change the cap length add

            Code:
            whiskers(recast(rcap) msize(*2))
            or whatever.
            This works! Thank you so much, Nick!

            Comment

            Working...
            X