Announcement

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

  • Labelling graphs directly (vertical orientation).

    Hello, I have the following dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float ln_epw
     8.505721
     7.437201
     9.702305
     9.538844
     7.009986
     7.814476
     9.241785
     7.599996
     7.759542
     6.759835
     9.597278
     7.726066
     6.309918
     8.066607
     7.974619
     7.425327
     7.973423
     8.160519
     8.910235
     7.731011
     7.305271
     8.377105
     7.519163
     7.294528
      8.07072
    10.981047
     7.625393
     8.020644
     7.338816
     8.377583
     8.008064
            .
     7.272729
     7.428493
     7.970263
     8.129428
     7.556132
     9.278922
      7.06104
     8.406191
     7.363429
     8.339566
     8.229511
     8.058133
     8.511058
     7.524602
    12.427462
     8.235419
     7.953169
     7.090077
     7.378916
      8.02643
     6.838162
     8.207694
     8.138704
     9.887652
     9.324944
     8.346511
     5.646624
      7.86601
     7.584902
     7.605793
     8.217089
     7.495542
      9.33305
     7.226872
     7.446752
     6.214608
     6.812445
     7.091234
     8.691147
     6.339771
     8.437664
     7.333221
     7.955872
     7.627792
      8.08215
     7.812996
      7.71338
     6.959188
     7.368619
     7.661527
     7.867531
     8.818121
      7.83354
     7.718686
      8.74319
      7.98227
     8.304725
     9.147283
     7.531909
     8.229511
     7.546581
     7.495542
      8.18208
     8.434762
     8.178867
     8.040269
     8.021804
    end

    I'm creating this graph (code below) , the distribution will look slightly off since the data is only a sample but I think the point remains clear.

    Code:
    twoway (kdensity ln_epw if owner==3 & region==1, lcolor(dknavy) lwidth(medthick)), ///
    ytitle("Density") xtitle("Ln monthly earnings per worker") xlabel(5(1)12) xsc(range(4.5 12.5)) ///
    xline(6.9077553, lwidth(medthick) lpattern(dash) lcolor(red))
    I would like to add to my graph a label which is oriented vertically and lies along the top portion of the red dashed line saying "Min. wage" I know its possible to do this using the graph editor but I would like to do it within the code itself.

    Please could you help me implement this. Something like this:

    [ATTACH=CONFIG]n1758948[/ATTACH]


    Thanks,
    Jad

  • #2
    Oops, here is the image that should be on the end of the message above.


    Click image for larger version

Name:	Screenshot 2024-07-16 at 10.57.55 PM.png
Views:	1
Size:	194.8 KB
ID:	1758951

    Comment


    • #3
      I have comments on several levels here. The code below will show one way to get your added text, but I've raised some different issues.

      Your graph code won't run directly as the two variables mentioned in the if qualifier aren't in the data example.

      That's easy to fix, but there is a deeper problem. With your density estimation you're just accepting the default choices of kernel type and width which aren't chosen very smartly by Stata. When used carefully kernel density estimation is a wonderful technique but with small samples as here any lumpiness in the tails is often just some side-effect of individual data values and density estimates aren't easy to interpret correctly. The trade-off between smoothing too much and smoothing too little is delicate and difficult and there isn't necessarily a good compromise.

      Some people address this by adding a marginal rug of observed values.

      Even when researchers find log wage a natural [indeed] or at least appropriate scale, I've yet to meet anybody who can exponentiate in their heads. So, I recommend showing values in your unstated currency. I used mylabels from the Stata Journal as a helper.

      As a tiny detail ln has been one standard notation for natural logarithm for over a century but I would never show Ln myself. There is no reason for any preference for capitalisation to override standard mathematical notation, any more than one should use Sin() not sin() or Exp() not exp(). My second and third graphs sidestep that small choice any way.


      Code:
      SJ-24-1 gr0092_1  . . . . . . . . . . . . . . . . Software update for mylabels
              (help nicelabels, mylabels, myticks if installed) . . . . .  N. J. Cox
              Q1/24   SJ 24(1):182--184
              fixes a bug that could bite if the options myscale() and
              clean were specified together
      
      SJ-22-4 gr0092  . . . . . . . . . . . . Speaking Stata: Automating axis labels
              (help nicelabels, mylabels, myticks if installed) . . . . .  N. J. Cox
              Q4/22   SJ 22(4):975--995
              provides commands to handle two common problems with graph
              axis labels: decide in advance on some "nice" numbers to
              use on one or both axes and show particular labels on some
              transformed scale
      The last graph shows a quantile plot which offers both the detail of individual values in the tails and some smoothing by virtue of cumulation. I used qplot from the Stata Journal.

      The latest public version was released in 2019.

      Code:
      SJ-19-3 gr42_8  . . . . . . . . . . . . . . . . . .  Software update for qplot
              (help qplot if installed) . . . . . . . . . . . . . . . . .  N. J. Cox
              Q3/19   SJ 19(3):748--751
              graph defaults have been tweaked and help file has been
              extended

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float ln_epw
       8.505721
       7.437201
       9.702305
       9.538844
       7.009986
       7.814476
       9.241785
       7.599996
       7.759542
       6.759835
       9.597278
       7.726066
       6.309918
       8.066607
       7.974619
       7.425327
       7.973423
       8.160519
       8.910235
       7.731011
       7.305271
       8.377105
       7.519163
       7.294528
        8.07072
      10.981047
       7.625393
       8.020644
       7.338816
       8.377583
       8.008064
              .
       7.272729
       7.428493
       7.970263
       8.129428
       7.556132
       9.278922
        7.06104
       8.406191
       7.363429
       8.339566
       8.229511
       8.058133
       8.511058
       7.524602
      12.427462
       8.235419
       7.953169
       7.090077
       7.378916
        8.02643
       6.838162
       8.207694
       8.138704
       9.887652
       9.324944
       8.346511
       5.646624
        7.86601
       7.584902
       7.605793
       8.217089
       7.495542
        9.33305
       7.226872
       7.446752
       6.214608
       6.812445
       7.091234
       8.691147
       6.339771
       8.437664
       7.333221
       7.955872
       7.627792
        8.08215
       7.812996
        7.71338
       6.959188
       7.368619
       7.661527
       7.867531
       8.818121
        7.83354
       7.718686
        8.74319
        7.98227
       8.304725
       9.147283
       7.531909
       8.229511
       7.546581
       7.495542
        8.18208
       8.434762
       8.178867
       8.040269
       8.021804
      end
      
      twoway kdensity ln_epw, lcolor(dknavy) lwidth(medthick) ///
      ytitle("Density") xtitle("Ln monthly earnings per worker") xlabel(5(1)12) xsc(range(4.5 12.5)) ///
      xline(6.9077553, lwidth(medthick) lpattern(dash) lcolor(red)) ///
      || scatteri 0.3 6.65 "Minimum wage", mlabangle(90) mlabsize(medium) legend(off) name(G1, replace)
      
      mylabels 300 1000 3000 10000 30000 100000, myscale(ln(@)) local(xla)
      
      twoway kdensity ln_epw, lcolor(dknavy) lwidth(medthick) ///
      ytitle("Density") xtitle("Monthly earnings per worker (log scale)") note(explain currency somewhere) xlabel(5(1)12) xsc(range(4.5 12.5)) xla(`xla') ///
      xline(6.9077553, lwidth(medthick) lpattern(dash) lcolor(red)) ///
      || scatteri 0.3 6.65 "Minimum wage", mlabangle(90) mlabsize(medium) legend(off) name(G2, replace)
      
      qplot ln_epw, mcolor(dknavy) ///
      ytitle("Monthly earnings per worker (log scale)") note(explain currency somewhere, size(medium) pos(11))  yla(`xla') ///
      yline(6.9077553, lwidth(medthick) lpattern(dash) lcolor(red)) xla(0 "0" 1 "1" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75") ///
      xtitle(Cumulative probability) addplot(scatteri 6.7 0.1 "Minimum wage 1000", ms(none) mlabsize(medium)) legend(off) name(G3, replace)

      Click image for larger version

Name:	tamini_G1.png
Views:	1
Size:	33.1 KB
ID:	1758985
      Click image for larger version

Name:	tamini_G2.png
Views:	1
Size:	34.7 KB
ID:	1758986
      Click image for larger version

Name:	ramini_G3.png
Views:	1
Size:	42.3 KB
ID:	1758987

      Comment


      • #4
        Thanks Nick! This is all very useful for me!

        Comment

        Working...
        X