Announcement

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

  • Adding title and note to my graph is completely ruining it. Why?

    Hi, here is what my raw data looks like:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str52 State str80 District float(Year total_crimes_against_women_pc caw_per100k l_num_accounts_pc high_fi_2011)
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2011 .000027142934 2.7142935     -.247201 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2017  .00002621849  2.621849   -.12876032 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2018             0         0            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2023             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2013  .00013415696 13.415696   -.26152954 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2012  .00005394301  5.394301   -.28941426 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2025             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2014  .00005338546  5.338546     -.149505 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2010             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2022  .00012848365 12.848365            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2009             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2019  .00005204073  5.204073            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2015   .0001589244  15.89244   -.11691732 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2021  .00007747564  7.747564            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2020  .00005177988  5.177988            . 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2016  .00015811356 15.811356   -.12218125 1
    "ANDAMAN & NICOBAR ISLANDS" "NICOBAR"                  2024             .         .   -.25685817 1
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2018  .00024635548  24.63555 -.0019154404 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2012   .0000846914  8.469139    -.5287375 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2025             .         .            . 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2019 .000072626484  7.262649    .00966099 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2009             .         .            . 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2011             .         .    -.6363877 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2023             .         .    .03298301 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2015   .0004990278  49.90278   -.08879615 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2020  .00004516403 4.5164027   -.04440651 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2010             .         .            . 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2016   .0004045406  40.45406  -.034680154 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2013  .00024339305 24.339306    -.4346214 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2024             .         .   .065826654 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2014   .0003725157  37.25157   -.14623222 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2021   .0001531738  15.31738    -.0306031 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2022  .00008068857  8.068856  -.009421785 0
    "ANDAMAN & NICOBAR ISLANDS" "NORTH AND MIDDLE ANDAMAN" 2017  .00024698075 24.698074   -.02030408 0
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2022   .0002146737  21.46737     .6928526 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2016  .00022830376 22.830376     .6020998 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2014   .0003138439  31.38439     .4958965 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2021  .00016380794 16.380795     .6315288 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2023             .         .      .735493 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2018  .00020634035 20.634035     .6448488 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2012   .0001502155 15.021552    .27120212 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2015   .0003442118  34.42118     .5503811 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2010             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2024             .         .     .7525097 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2019  .00012881623 12.881623     .6554139 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2009             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2025             .         .            . 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2013   .0002947194  29.47194     .3460594 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2017  .00017035863 17.035864     .6253322 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2011             .         .     .1667892 1
    "ANDAMAN & NICOBAR ISLANDS" "SOUTH ANDAMAN"            2020  .00007610126  7.610126    .58320284 1
    "ANDHRA PRADESH"            "ADILABAD"                 2016             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2018             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2019             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2012   .0009144312  91.44312     .8007309 1
    "ANDHRA PRADESH"            "ADILABAD"                 2011   .0003126324 31.263235    -.4456643 1
    "ANDHRA PRADESH"            "ADILABAD"                 2015             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2014             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2025             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2023             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2021             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2022             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2020             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2017             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2024             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2009             .         .            . 1
    "ANDHRA PRADESH"            "ADILABAD"                 2013   .0009281022  92.81023    1.0181375 1
    "ANDHRA PRADESH"            "ADILABAD"                 2010             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2021             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2010             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2023             .         .    -.3022683 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2019             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2018             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2022  .00007596445  7.596446    -.3692969 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2013             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2024             .         .    -.2972836 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2017             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2016             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2009             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2025             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2020             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2014             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2012             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2015             .         .            . 1
    "ANDHRA PRADESH"            "ALLURI SITHARAMA RAJU"    2011             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2025             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2010             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2017             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2016             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2012             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2009             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2011             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2015             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2023             .         .     .6611728 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2013             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2019             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2021             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2018             .         .            . 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2024             .         .     .6721603 1
    "ANDHRA PRADESH"            "ANAKAPALLI"               2020             .         .            . 1
    end
    format %ty Year


    I am using code to graph total crimes against women by 100,000 persons vs the log of number of deposit accounts in per capita:
    Code:
    *one-liner rescale (temporary variable)
    capture drop caw_per100k
    gen caw_per100k = 100000*total_crimes_against_women_pc
    
    quietly summarize l_num_accounts_pc, detail
    local p1 = r(p1)
    local p99 = r(p99)
    
    quietly summarize caw_per100k if inrange(l_num_accounts_pc, `p1', `p99'), detail
    local ymax = r(p99)
    local inc  = 20                    // ticks every 20 per 100k (adjust if needed)
    local top  = ceil(`ymax'/`inc')*`inc'
    
    twoway (lowess caw_per100k l_num_accounts_pc if high_fi_2011==0 & inrange(l_num_accounts_pc, `p1', `p99'), bwidth(0.7) lcolor(navy) lwidth(medthick)) ///
           (lowess caw_per100k l_num_accounts_pc if high_fi_2011==1 & inrange(l_num_accounts_pc, `p1', `p99'), bwidth(0.7) lcolor(cranberry) lwidth(medthick)), ///
           legend(label(1 "Low FI at baseline (2011)") label(2 "High FI at baseline (2011)") pos(12) ring(1) col(1) region(lstyle(none)) size(small)) ///
           xtitle("log(Deposit accounts per capita)") ///
           ytitle("Crimes against women (per 100,000)") ///
           yscale(range(0 `top')) ///
           ylabel(0(`inc') `top', grid glcolor(gs12) glwidth(vthin)) ///
           xlabel(, grid glcolor(gs12) glwidth(vthin)) ///
           graphregion(color(white) margin(4 18 6 6)) plotregion(margin(2 2 2 2)) aspect(0.9)
    to produce my graph which looks absolutely fine and how I want it to. But the moment I try adding a title and note in it - it detaches y axis completely from the graph and I just cannot understand why. I have tried everything from changing margins to giving the graph more/less space on the left. I cannot understand why this is happening. The code I use when I add title and note is:

    Code:
    *Trim X to p1–p99
    quietly summarize l_num_accounts_pc, detail
    local p1  = r(p1)
    local p99 = r(p99)
    
    *Y-axis (per 100k): nice top & tick step
    quietly summarize caw_per100k if inrange(l_num_accounts_pc, `p1', `p99'), detail
    local inc = 10
    local top = ceil(r(p99)/`inc')*`inc'
    
    set scheme s1color
    
    twoway ///
     (lowess caw_per100k l_num_accounts_pc if high_fi_2011==0 & inrange(l_num_accounts_pc, `p1', `p99'), ///
            bwidth(0.7) lcolor(navy) lwidth(medthick)) ///
     (lowess caw_per100k l_num_accounts_pc if high_fi_2011==1 & inrange(l_num_accounts_pc, `p1', `p99'), ///
            bwidth(0.7) lcolor(cranberry) lwidth(medthick)) ///
    , legend(label(1 "Low FI at baseline (2011)") label(2 "High FI at baseline (2011)") ///
             pos(12) ring(1) col(1) region(lstyle(none)) size(small)) ///
      title("Crimes vs Financial Inclusion (Baseline split: 2011)", size(medsmall)) ///
      note("Outcome in crimes per 100,000; LOWESS bwidth = 0.7; X trimmed at p1–p99", size(vsmall)) ///
      xtitle("log(Deposit accounts per capita)", size(small)) ///
      ytitle("Crimes against women (per 100,000)", size(small)) ///
      yscale(range(0 `top')) ///
      ylabel(0(`inc')`top', grid glcolor(gs12) glwidth(vthin) labsize(small)) ///
      xlabel(, grid glcolor(gs12) glwidth(vthin) labsize(small)) ///
      graphregion(color(white) margin(4 18 6 6)) ///
      plotregion(margin(2 2 2 2)) ///
      aspect(0.9) xsize(7) ysize(5)
    Please help me identify and fix the issue as I am out of ideas. Thank you. This graph is going to go in a published article and I need it to look like professional journal ready graph.

    Thank you for everyone's help in advance!
    Last edited by anisha arya; 12 Sep 2025, 08:46.

  • #2
    I can't explain it any detail, because I don't really understand why it is happening, but the problem goes away if you remove the -aspect(0.9)- option from your command. Of course, then the graph comes out appreciably wider.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      I can't explain it any detail, because I don't really understand why it is happening, but the problem goes away if you remove the -aspect(0.9)- option from your command. Of course, then the graph comes out appreciably wider.
      Thank you Clyde, at least I have a title now even if it is bigger. This really helped. Additional question, do you know how to make the y axis start at 0?

      Comment


      • #4
        Additional question, do you know how to make the y axis start at 0?
        No, I don't know how to do this. I'm not even sure it is possible. It might be possible to "trick" Stata into doing this by specifying the axis lines to be invisible and then including -xline()- and -yline()- options at appropriate values, but I don't really know if that even works.

        Comment


        • #5
          It’s a bit strange that you specify both aspect() as well as xsize() and ysize(). Specifying the latter two already implies an aspect ratio, and vice versa. The axis detaches at an aspect ratio greater than 0.7, so you can limit it to that. As for the y-axis starting point, I think you introduce a margin through your plotregion() specification. You can constrain this to zero.

          Code:
          capture drop caw_per100k
          gen caw_per100k = 100000*total_crimes_against_women_pc
          
          quietly summarize l_num_accounts_pc, detail
          local p1 = r(p1)
          local p99 = r(p99)
          
          quietly summarize caw_per100k if inrange(l_num_accounts_pc, `p1', `p99'), detail
          local ymax = r(p99)
          local inc  = 20                    // ticks every 20 per 100k (adjust if needed)
          local top  = ceil(`ymax'/`inc')*`inc'
          
          twoway (lowess caw_per100k l_num_accounts_pc if high_fi_2011==0 & inrange(l_num_accounts_pc, `p1', `p99'), bwidth(0.7) lcolor(navy) lwidth(medthick)) ///
                 (lowess caw_per100k l_num_accounts_pc if high_fi_2011==1 & inrange(l_num_accounts_pc, `p1', `p99'), bwidth(0.7) lcolor(cranberry) lwidth(medthick)), ///
                 legend(label(1 "Low FI at baseline (2011)") label(2 "High FI at baseline (2011)") pos(12) ring(1) col(1) region(lstyle(none)) size(small)) ///
                 xtitle("log(Deposit accounts per capita)") ///
                 ytitle("Crimes against women (per 100,000)") ///
                 yscale(range(0 `top')) ///
                 ylabel(0(`inc') `top', grid glcolor(gs12) glwidth(vthin)) ///
                 xlabel(, grid glcolor(gs12) glwidth(vthin)) ///
                 graphregion(color(white) margin(4 18 6 6)) plotregion(margin(2 2 2 2)) aspect(0.9)
          
          quietly summarize l_num_accounts_pc, detail
          local p1  = r(p1)
          local p99 = r(p99)
          
          *Y-axis (per 100k): nice top & tick step
          quietly summarize caw_per100k if inrange(l_num_accounts_pc, `p1', `p99'), detail
          local inc = 10
          local top = ceil(r(p99)/`inc')*`inc'
          
          set scheme s1color
          
          twoway ///
           (lowess caw_per100k l_num_accounts_pc if high_fi_2011==0 & inrange(l_num_accounts_pc, `p1', `p99'), ///
                  bwidth(0.7) lcolor(navy) lwidth(medthick)) ///
           (lowess caw_per100k l_num_accounts_pc if high_fi_2011==1 & inrange(l_num_accounts_pc, `p1', `p99'), ///
                  bwidth(0.7) lcolor(cranberry) lwidth(medthick)) ///
          , legend(label(1 "Low FI at baseline (2011)") label(2 "High FI at baseline (2011)") ///
                   pos(12) ring(1) col(1) region(lstyle(none)) size(small)) ///
            title("Crimes vs Financial Inclusion (Baseline split: 2011)", size(medsmall)) ///
            note("Outcome in crimes per 100,000; LOWESS bwidth = 0.7; X trimmed at p1–p99", size(vsmall)) ///
            xtitle("log(Deposit accounts per capita)", size(small)) ///
            ytitle("Crimes against women (per 100,000)", size(small)) ///
            yscale(range(0 `top')) ///
            ylabel(0(`inc')`top', grid glcolor(gs12) glwidth(vthin) labsize(small)) ///
            xlabel(, grid glcolor(gs12) glwidth(vthin) labsize(small)) ///
            graphregion(color(white) margin(4 18 6 6)) ///
             aspect(.7) plotregion(margin(zero))
          Res.:

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	58.0 KB
ID:	1781776

          Comment


          • #6
            Originally posted by Andrew Musau View Post
            It’s a bit strange that you specify both aspect() as well as xsize() and ysize(). Specifying the latter two already implies an aspect ratio, and vice versa. The axis detaches at an aspect ratio greater than 0.7, so you can limit it to that. As for the y-axis starting point, I think you introduce a margin through your plotregion() specification. You can constrain this to zero.

            Code:
            capture drop caw_per100k
            gen caw_per100k = 100000*total_crimes_against_women_pc
            
            quietly summarize l_num_accounts_pc, detail
            local p1 = r(p1)
            local p99 = r(p99)
            
            quietly summarize caw_per100k if inrange(l_num_accounts_pc, `p1', `p99'), detail
            local ymax = r(p99)
            local inc = 20 // ticks every 20 per 100k (adjust if needed)
            local top = ceil(`ymax'/`inc')*`inc'
            
            twoway (lowess caw_per100k l_num_accounts_pc if high_fi_2011==0 & inrange(l_num_accounts_pc, `p1', `p99'), bwidth(0.7) lcolor(navy) lwidth(medthick)) ///
            (lowess caw_per100k l_num_accounts_pc if high_fi_2011==1 & inrange(l_num_accounts_pc, `p1', `p99'), bwidth(0.7) lcolor(cranberry) lwidth(medthick)), ///
            legend(label(1 "Low FI at baseline (2011)") label(2 "High FI at baseline (2011)") pos(12) ring(1) col(1) region(lstyle(none)) size(small)) ///
            xtitle("log(Deposit accounts per capita)") ///
            ytitle("Crimes against women (per 100,000)") ///
            yscale(range(0 `top')) ///
            ylabel(0(`inc') `top', grid glcolor(gs12) glwidth(vthin)) ///
            xlabel(, grid glcolor(gs12) glwidth(vthin)) ///
            graphregion(color(white) margin(4 18 6 6)) plotregion(margin(2 2 2 2)) aspect(0.9)
            
            quietly summarize l_num_accounts_pc, detail
            local p1 = r(p1)
            local p99 = r(p99)
            
            *Y-axis (per 100k): nice top & tick step
            quietly summarize caw_per100k if inrange(l_num_accounts_pc, `p1', `p99'), detail
            local inc = 10
            local top = ceil(r(p99)/`inc')*`inc'
            
            set scheme s1color
            
            twoway ///
            (lowess caw_per100k l_num_accounts_pc if high_fi_2011==0 & inrange(l_num_accounts_pc, `p1', `p99'), ///
            bwidth(0.7) lcolor(navy) lwidth(medthick)) ///
            (lowess caw_per100k l_num_accounts_pc if high_fi_2011==1 & inrange(l_num_accounts_pc, `p1', `p99'), ///
            bwidth(0.7) lcolor(cranberry) lwidth(medthick)) ///
            , legend(label(1 "Low FI at baseline (2011)") label(2 "High FI at baseline (2011)") ///
            pos(12) ring(1) col(1) region(lstyle(none)) size(small)) ///
            title("Crimes vs Financial Inclusion (Baseline split: 2011)", size(medsmall)) ///
            note("Outcome in crimes per 100,000; LOWESS bwidth = 0.7; X trimmed at p1–p99", size(vsmall)) ///
            xtitle("log(Deposit accounts per capita)", size(small)) ///
            ytitle("Crimes against women (per 100,000)", size(small)) ///
            yscale(range(0 `top')) ///
            ylabel(0(`inc')`top', grid glcolor(gs12) glwidth(vthin) labsize(small)) ///
            xlabel(, grid glcolor(gs12) glwidth(vthin) labsize(small)) ///
            graphregion(color(white) margin(4 18 6 6)) ///
            aspect(.7) plotregion(margin(zero))
            Res.:

            [ATTACH=CONFIG]n1781776[/ATTACH]
            This worked exactly how I wanted it, thank you so much! I set aspect to 0.6 and it is looking great now! Thank again.

            Comment

            Working...
            X