Announcement

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

  • Manual x-axis labels for boxplots?

    Hello--

    I've been trying to find ways to place text labels below vertical box plots. An example of my data is below. When I use this:

    Code:
    graph box Cath Echo, legend(off) title("Superior Vena Cava Values") ytitle ("Peak Pressure in mm Hg")
    ...instead of a legend I would prefer to have the words "Cath" and "Echo" as x-axis labels (for lack of a better description). I know I could use Graph Editor but I was hoping there would be code that I can use instead. Any suggestions?


    Code:
     input float(Cath Echo)   8.9    9    5  5.3 13.7 14.5 11.6 11.8 10.3 11.6  4.6  3.8  end

  • #2
    A short answer is to use the ascategory option. A longer answer is to guess that these data come paired, so that looking at marginal distributions alone loses information. Also, looking at box plots alone loses information even about marginal distributions. I will guess wildly that you have somewhere in the ballpark 30 100 300 patients and there is some interesting and important detail there too.

    Here is what you are asking for and two more takes. Giving boxes Tufte form -- for more on that see https://www.statalist.org/forums/for...-without-boxes and its references -- is a choice, and more traditional boxes are easily programmable (but my strong suggestion is make the boxes thin, as fat boxes carry no more information and are most easily misread).

    I used Stata 18, but essentially the same graphs can be produced in several earlier versions.

    Code:
    clear 
    
    input float(Cath Echo)   
    8.9    9    
    5  5.3 
    13.7 14.5 
    11.6 11.8 
    10.3 11.6  
    4.6  3.8  
    end
    
    local opts legend(off) aspect(1.4) title("Superior Vena Cava Values") ytitle("Peak Pressure in mm Hg")
    
    graph box Cath Echo,  ascategory `opts' name(G1, replace)
    
    gen x1 = 1 
    gen x2 = 2 
    
    twoway pcspike Cath x1 Echo x2, xla(1 "Cath" 2 "Echo", tlc(none)) xtitle("") `opts' name(G2, replace)
    
    su Cath, detail 
    
    foreach stat in min max p25 p50 p75 {
        local C`stat' = r(`stat')
    }
    
    su Echo, detail 
    
    foreach stat in min max p25 p50 p75 {
        local E`stat' = r(`stat')
    }
    
    local x0 = 0.8 
    local x3 = 2.2 
    
    twoway pcspike Cath x1 Echo x2, xla(1 "Cath" 2 "Echo", tlc(none)) xtitle("") `opts' ///
    || scatteri `Cmin' `x0' `Cp25' `x0', recast(line) lc(black) ///
    || scatteri `Cmax' `x0' `Cp75' `x0', recast(line) lc(black) ///
    || scatteri `Cp50' `x0', ms(Dh) mc(black) /// 
    || scatteri `Emin' `x3' `Ep25' `x3', recast(line) lc(black) ///
    || scatteri `Emax' `x3' `Ep75' `x3', recast(line) lc(black) ///
    || scatteri `Ep50' `x3', ms(Dh) mc(black) /// 
    name(G3, replace)

    Click image for larger version

Name:	venacava_G1.png
Views:	1
Size:	21.0 KB
ID:	1712645
    Click image for larger version

Name:	venacava_G2.png
Views:	1
Size:	30.2 KB
ID:	1712646
    Click image for larger version

Name:	venacava_G3.png
Views:	1
Size:	28.5 KB
ID:	1712647





    Comment


    • #3
      Naturally other takes are possible such as a scatter plot Cath versus Echo, or a plot of (Cath - Echo) versus (Cath + Echo)/2

      More at

      https://journals.sagepub.com/doi/pdf...867X0400400309

      https://www.sciencedirect.com/scienc...740?via%3Dihub

      https://journals.sagepub.com/doi/pdf/10.1177/1536867X0900900408

      Last edited by Nick Cox; 08 May 2023, 01:22.

      Comment


      • #4
        This delivers on the idea that more conventional box plots are possible too. Hence (at the risk or excessive detail) I add point symbols for means too (and in turn it could be that for pressures a geometric mean would have an edge).

        Code:
        clear 
        
        input float(Cath Echo)   
        8.9    9    
        5  5.3 
        13.7 14.5 
        11.6 11.8 
        10.3 11.6  
        4.6  3.8  
        end
        
        local opts legend(off) aspect(1.4) title("Superior Vena Cava Values") ytitle("Peak Pressure in mm Hg")
        
        graph box Cath Echo,  ascategory `opts' name(G1, replace)
        
        gen x1 = 1 
        gen x2 = 2 
        
        twoway pcspike Cath x1 Echo x2, xla(1 "Cath" 2 "Echo", tlc(none)) xtitle("") `opts' name(G2, replace)
        
        su Cath, detail 
        
        foreach stat in min max p25 p50 p75 mean{
            local C`stat' = r(`stat')
            tempvar C_`stat' 
            gen `C_`stat'' = r(`stat')
        }
        
        su Echo, detail 
        
        foreach stat in min max p25 p50 p75 mean {
            local E`stat' = r(`stat')
            tempvar E_`stat' 
            gen `E_`stat'' = r(`stat')
        }
        
        tempvar X0 X3 
        local x0 = 0.8
        gen `X0' = 0.8  
        local x3 = 2.2 
        gen `X3' = 2.2 
        
        twoway pcspike Cath x1 Echo x2, xla(1 "Cath" 2 "Echo", tlc(none)) xtitle("") `opts' ///
        || scatteri `Cmin' `x0' `Cp25' `x0', recast(line) lc(black) ///
        || scatteri `Cmax' `x0' `Cp75' `x0', recast(line) lc(black) ///
        || rbar `C_p50' `C_p75' `X0', barw(0.1) fcolor(none) lcolor(black) /// 
        || rbar `C_p50' `C_p25' `X0', barw(0.1) fcolor(none) lcolor(black) /// 
        || scatteri `Emin' `x3' `Ep25' `x3', recast(line) lc(black) ///
        || scatteri `Emax' `x3' `Ep75' `x3', recast(line) lc(black) ///
        || rbar `E_p50' `E_p75' `X3 ', barw(0.1) fcolor(none) lcolor(black) /// 
        || rbar `E_p50' `E_p25' `X3', barw(0.1) fcolor(none) lcolor(black) /// 
        || scatteri `Cmean' `x0', ms(Dh) mc(black) /// 
        || scatteri `Emean' `x3', ms(Dh) mc(black) /// 
        name(G3, replace)
        Click image for larger version

Name:	vencava_G3a.png
Views:	1
Size:	30.5 KB
ID:	1712937

        Comment


        • #5
          Sorry for my delayed response. These are amazing, thanks so much, Nick. I have Stata 18 as well so I'll be able to adapt your suggestions.

          Comment

          Working...
          X