Announcement

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

  • Break in both x and y axis

    Dear Statalist users,

    I am having trouble creating a break in the axis of a graph. I have tried to use https://www.stata.com/support/faqs/g.../scale-breaks/ but I am having trouble creating the break and combining the graphs with a break in both x and y axes.

    This is the code I am currently using. I am trying to make a break so the data points from 0-80 are the majority of the graph and then points at 120 and 240 are on the end with breaks.

    I have also attached an image of the graph and I am more than happy to share the date to work with.

    Code:
            drop if Objective!="A00"
            drop if Processingdelay==4 | NfL==.
            keep NfL ID_visit sample_delay disease_grp
            reshape wide NfL , i(ID_visit) j(sample_delay) string //creates variables of sample_type Ven_p Ven_S Cap_P Cap_S for scatter plots
    
            ds ID* dis* , not // lists all string variables in memory of  Ven_p Ven_S Cap_P Cap_S
            local Obj_type `r(varlist)' // local for variables  Ven_p Ven_S Cap_P Cap_S
            
            local axislabelformat labsize(medium) tlwidth(medium)
            local axislineformat aspectratio(1) xscale(lwidth(vthin)) yscale(lwidth(vthin))
            local scattercircle msymbol(circle) msize(large) mlcolor(none)
            
            include "0.1 GRAPHICS_OD.do" //contains colours for scatter plots
            
        foreach s_type1 in `Obj_type' { //** Open double loop to run through code comparing each sample_type in varlist with each other in list
            foreach s_type2 in `Obj_type'  {
                
                
            twoway (lfitci `s_type1' `s_type2', clwidth(vthin) clcolor("`INF_Blue_Light'")) ///
                (scatter `s_type1' `s_type2' if disease_grp==0 , `scattercircle' mcolor("`INF_grey'")) ///
                (scatter `s_type1' `s_type2' if disease_grp==1 , `scattercircle' mcolor("`INF_Red_Light'")) ///
                (scatter `s_type1' `s_type2' if disease_grp==2 , `scattercircle' mcolor("`INF_Red'")) ///
                (scatter `s_type1' `s_type2' if disease_grp==3 , `scattercircle' mcolor("`INF_Indigo_Light'")) ///
                (scatter `s_type1' `s_type2' if disease_grp==4 , `scattercircle' mcolor("`INF_Indigo'")) ///    
                (scatter `s_type1' `s_type2' if disease_grp==5 , `scattercircle' mcolor("`INF_Indigo_Dark'")) ///    
                (scatter `s_type1' `s_type2' if disease_grp==6 , `scattercircle' mcolor("`INF_Green_Light'")) ///
                (scatter `s_type1' `s_type2' if disease_grp==7 , `scattercircle' mcolor("`INF_green'")) ///    
                (scatter `s_type1' `s_type2' if disease_grp==8 , `scattercircle' mcolor("`INF_gold'")) ///    
                (function y=x , range(``obj'`analyte'y') lcolor("`INF_Blue'") lwidth(vthin)), ///
                legend(off) ytitle("`s_type1'", size(medthick)) xtitle("`s_type2'", size(medthick)) ///
                xlabel("0(20)80", `axislabelformat') xscale(range("0 80")) ///
                ylabel("0(20)80", `axislabelformat') yscale(range("0 80")) ///
                `axislineformat' ///
                name(`s_type1'_`s_type2'_Scat,replace)
              
            }
            
        }
    I look forward to your help and responses. Please do let me know if you require more information.

    Annabelle
    Click image for larger version

Name:	Screenshot 2023-10-25 at 11.38.50.png
Views:	1
Size:	224.7 KB
ID:	1731464
    Last edited by Annabelle Coleman; 25 Oct 2023, 06:34.

  • #2
    Why not use a log scale for both y and x?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Sorry, but I can't follow everything you're doing here and indeed -- as you say -- much of the code and all of your data are hidden from us.

      I can't see that you're trying to implement a scale break, so what is the problem otherwise?

      Evidently the text with regression results is badly placed and you're missing how to show superscript 2, and so forth. I can't quickly see the associated code.

      Someone on social media once commented that the FAQ you cite (I'm first author) was intended to make you feel guilty about wanting a scale break. That was sharp and exaggerated but not absolutely wrong. Scale breaks in my view are usually a bad idea and Stata doesn't really support them. Anything you do is a work-around.

      More fundamentally I don't off-hand know how to break both axes at once in a simple way, as that implies four sub-graphs.

      How about using logarithmic scales? Or one graph with all of the data and one omitting the moderate outliers?

      As commented recently red and green are best not shown together on a graph.

      Distinguishing 9 groups clearly is a hard task without something different, such as a front-and-back plot. https://www.statalist.org/forums/for...ailable-on-ssc

      I don't know what NfLCap_S_0 or NfLCap_P_0 is and presumably when you get this graph improved you will define variable labels or equivalently better axis titles.

      EDIT I see that Maarten Buis has also suggested logarithmic scales.

      Comment


      • #4
        Nick Cox is aware that he and I have an amicable gentlemen's disagreement on this topic.

        For me the issue often comes down to a tradeoff among (a) displaying the data "as is" versus (b) using some method (e.g. log transformation) that will pull in the extreme data points but will also visually compress the non-extreme data cloud versus (c) using an axis break that provides a compromise between the two. I believe that reasonable people can disagree about how they prefer to manage the tradeoff.

        I wrote a little note last year that should give you some idea of how to introduce axis breaks in Stata graphs. https://uwmadison.box.com/s/kt0lsncj...ye54wka5j0f3rb

        Comment


        • #5
          Indeed. as many serious researchers have wanted to show scale breaks, I am happy to think that they are otherwise smart people.

          The purpose of visualization is to make things clear. As John knows, I see quite often boundary cases where to me visualization requires something a bit awkward like log(y + c) even though that is not the best way towards a model.

          A scale break is easy to explain but harder to assimilate. But then some people tell me that log scales are hard to assimilate.

          Comment


          • #6
            "otherwise"

            Comment


            • #7
              There are scale breaks and scale breaks. Like many people of my generation or any older or some younger, I drew many graphs by hand long before I ever used a computer. We were encouraged, indeed instructed, that a y axis that didn't start at zero should be indicated as such by some kind of jagged line. These are no longer fashionable or even easy in many programs. +

              The scale breaks here are within the range of the data and a different kettle of fish.

              According to legend, the late statistician David Wallace called "take logarithms" the first rule of applied statistics. (I wish I could remember where I saw this.)

              Comment


              • #8
                Axis breaks and log-transforms may need to take a back seat to visualizations like this.

                Click image for larger version

Name:	bar chart.jpg
Views:	1
Size:	312.1 KB
ID:	1788196


                posted on Bluesky at https://bsky.app/profile/spavel.bsky.../3n2hhgvvfek2n

                Comment


                • #9
                  Dear John,
                  Thank you for this most remarkable graph! I am not sure if we should laugh or cry!
                  Visiting the Bluesky blog, however, does make me think again.
                  The Dubois examples are of interest, maybe there is a place for 'snaked' bar and circle plots in the data visualization universe...
                  Last edited by ericmelse; 17 Nov 2026, 12:27.
                  http://publicationslist.org/eric.melse

                  Comment


                  • #10
                    Du Bois was a great man and a fine scholar but some recent discussions of his graphical work lack a critical or historical context. Beyond that, and some people are going to disagree on this, the snakes and spirals he sometimes used may be amusing or striking, but that does not make them good choices. in my view the answer is more likely to be to use a logarithmic scale. The riposte that many people don't know logarithms is predictable, but you can use a logarithmic scale on graphs as long as you provide an explanation. Many graphs in good newspapers or other responsible news media used logarithmic scales successfully to convey explosive take-off in cases, hospitalizations or deaths during the Covid pandemic.

                    Comment


                    • #11
                      For a brief take on Du Bois see https://www.amazon.com/gp/customer-r...at_pdctrvw_srp

                      Comment


                      • #12
                        Click image for larger version

Name:	axis_overview_zoom.png
Views:	1
Size:	130.3 KB
ID:	1788234
                        Click image for larger version

Name:	axis_both_breaks.png
Views:	1
Size:	137.6 KB
ID:	1788235


                        For Annabelle’s original scatterplot, I would start with Nick’s suggestion of showing the full range alongside a 0–80 zoom. The overview keeps every pair and marks the zoom window. The zoom panel uses only pairs for which both measurements are within that window. The range() suboption cannot narrow the range of points already plotted, so the zoom uses an explicit if qualifier. Both panels show the line of equality. Stata axis-scale documentation

                        John’s note also suggests a way to put two breaks on each axis in a single panel. In the synthetic example below, both axes have empty intervals at (80, 110) and (130, 220). Applying the same transformation to x and y preserves equality and which measurement is larger. The diagonal is drawn in segments, one for each retained interval. Distances across a break cannot be read in the original units. Before using this on the NfL data, check that neither variable has observations in the proposed intervals. John Mullahy’s note

                        Nick’s review of the Du Bois portfolios makes a related point about audience. The public-outreach purpose of the 1900 exhibition gave Du Bois good reason to avoid log scales. Nick Cox’s review

                        The code uses synthetic data and runs with official Stata commands.

                        Code:
                        version 16.0
                        preserve
                        clear
                        
                        * Synthetic paired measurements. No patient data are used.
                        input double(x y)
                          0   0
                          5   8
                         10  14
                         13   9
                         18  20
                         20  16
                         24  29
                         28  24
                         31  35
                         35  30
                         40  44
                         43  38
                         47  49
                         50  45
                         54  59
                         61  54
                         65  69
                         71  64
                         74  77
                         79  73
                         80  80
                         65 116
                        122  75
                        111 114
                        118 126
                        124 119
                        130 130
                        125 235
                        229 121
                        232 239
                        240 231
                        248 246
                        end
                        
                        generate byte zoom = inrange(x,0,80) & inrange(y,0,80)
                        local teal "25 108 109"
                        local rust "159 72 37"
                        local gray "101 112 121"
                        local paper "255 255 255"
                        local axes scheme(s2color) legend(off) ///
                            xtitle("Measurement X") ytitle("Measurement Y") ///
                            graphregion(color("`paper'"))
                        local plot_style color("`paper'") lcolor(none)
                        
                        * 1. A full-range overview and the 0-80 window.
                        twoway (pci 80 0 80 80 80 80 0 80, lcolor("`teal'") lpattern(dash)) ///
                            (function y=x, range(0 250) lcolor("`gray'")) ///
                            (scatter y x if zoom, mcolor("`teal'") msize(small)) ///
                            (scatter y x if !zoom, mcolor("`rust'") msize(small)), ///
                            `axes' aspectratio(1) plotregion(`plot_style') ///
                            xscale(range(0 250)) yscale(range(0 250)) ///
                            xlabel(0(40)240, nogrid) ylabel(0(40)240, angle(horizontal) nogrid) ///
                            title("All pairs") name(ab_full, replace) nodraw
                        
                        twoway (function y=x, range(0 80) lcolor("`gray'")) ///
                            (scatter y x if zoom, mcolor("`teal'") msize(small)), ///
                            `axes' aspectratio(1) plotregion(`plot_style') ///
                            xscale(range(0 80)) yscale(range(0 80)) ///
                            xlabel(0(20)80, nogrid) ylabel(0(20)80, angle(horizontal) nogrid) ///
                            title("Both measurements 0-80") name(ab_zoom, replace) nodraw
                        
                        graph combine ab_full ab_zoom, rows(1) scheme(s2color) ///
                            xsize(9) ysize(5.5) graphregion(color("`paper'")) ///
                            title("Paired measurements at two scales") ///
                            note("Synthetic data. Dashed lines mark the zoom window. Gray line shows y = x.") ///
                            name(ab_overview_zoom, replace)
                        
                        * 2. Optional. Break (80,110) and (130,220) on both axes.
                        * The retained ranges map to [0,80], [88,108], and [116,146].
                        foreach v in x y {
                            assert inrange(`v',0,80) | inrange(`v',110,130) | inrange(`v',220,250)
                            generate double `v'_plot = `v' - 22*(`v'>=110) - 82*(`v'>=220)
                        }
                        assert sign(y_plot-x_plot) == sign(y-x)
                        
                        local ticks 0 20 40 60 80 98 "120" 136 "240"
                        twoway (pci 0 0 80 80 88 88 108 108 116 116 146 146, lcolor("`gray'")) ///
                            (scatter y_plot x_plot if zoom, mcolor("`teal'") msize(small)) ///
                            (scatter y_plot x_plot if !zoom, mcolor("`rust'") msize(small)) ///
                            (pci -5 -5 -5 80 -5 88 -5 108 -5 116 -5 146 ///
                                 -5 -5 80 -5 88 -5 108 -5 116 -5 146 -5, lcolor(black)) ///
                            (pci -7 82 -3 86 -7 110 -3 114 82 -7 86 -3 110 -7 114 -3, lcolor(black)), ///
                            `axes' aspectratio(1, placement(sw)) plotregion(`plot_style' margin(zero)) ///
                            xscale(noline range(-7 151)) yscale(noline range(-7 151)) ///
                            xlabel(`ticks', nogrid) ylabel(`ticks', angle(horizontal) nogrid) ///
                            xsize(6.2) ysize(6.7) title("The same pairs with both axes broken") ///
                            note("Synthetic data. Omitted on both axes: (80,110) and (130,220)." ///
                                 "Gray line segments show y = x within the retained ranges.") ///
                            name(ab_both_axes, replace)
                        
                        restore

                        Comment

                        Working...
                        X