Announcement

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

  • Plotting multiple concentration curves in one graph

    Hi everyone,

    I'm conducting some inequality analysis and using concentration indices and curves to depict inequalities in outcomes. I'm using the user contributed conindex command for this purpose. While I am able to produce these curves for a specific population, I have not been able to use it to graph the curves for multiple groups (e.g., by sex or age) in one graph. I tried using another user contributed command glcurve for this purpose, but was unable to get this to work properly.

    Below is the code I'm currently using to produce two curves for males and females in two separate graphs. How can I adjust this to produce both curves in the same graph? I've also included a sample of my data.

    Code:
    conindex pgsi [pweight=scq_wgt] if sex==1, rankvar(hheqinc) robust cluster(xwaveid) truezero bounded limits(0 27)erreygers graph
    conindex pgsi [pweight=scq_wgt] if sex==2, rankvar(hheqinc) robust cluster(xwaveid) truezero bounded limits(0 27)erreygers graph
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long xwaveid float(pgsi scq_wgt sex hheqinc)
    1500001  .         . 2 36191.332
    1500002  .         . 2 36191.332
    1500003  .         . 1 36191.332
    1500004  . 1336.3737 1  43259.33
    1500006  .         0 2   44685.5
    1500007  .         . 2   44685.5
    1500010  .         . 1 126668.66
    1500012  0 1209.0953 2  45549.33
    1500018  0  897.1745 2     34340
    1500023  0 1879.3213 1 67506.664
    1500024  .         0 1  35680.77
    1500025  0 1954.5593 2  100449.6
    1500028  0 1095.2823 2  49080.67
    1500031  .         . 2     44462
    1500034  0 1225.8156 2  80826.66
    1500035  0  694.3892 1  46307.78
    1500038  5 1395.4056 1 15129.333
    1500039  0 1581.5642 1  57854.67
    1500042  0 1588.5952 1   33541.6
    1500045  0  1817.037 1  48392.14
    1500046  .         . 1  48392.14
    1500047  .         . 1 28703.334
    1500051  0   737.827 2     63013
    1500053  0  2861.205 1 27301.785
    1500054  0  4960.978 2 27301.785
    1500055  0  4686.794 2  48662.82
    1500056  0 4799.8564 1  48662.82
    1500060  0   8072.21 2  48662.82
    1500062  0  810.4601 2     35148
    1500064  .         0 2  30672.22
    1500066  0  161.7171 1 12819.333
    1500068  .         . 1   94625.5
    1500069  .         . 2   94625.5
    1500070  .         0 2   53920.5
    1500071  0 1117.5151 2  65297.33
    1500072  0  2202.835 1 29333.334
    1500073  0  762.3912 2 34087.332
    1500076  0 1028.1517 2  40757.08
    1500078  0   1418.98 1 24608.666
    1500082  0  495.1196 2  60330.67
    1500083  .         . 1  56118.23
    1500084  .         . 2  56118.23
    1500086  4 1142.2218 1 37424.668
    1500087  .         . 1     20885
    1500088  .         . 2     20885
    1500089  0  368.1432 1     42028
    1500090 11 1230.8286 1  40152.67
    1500091  .         . 1     96545
    1500092  0 10203.215 2 35772.855
    1500094  .         . 2  93092.66
    1500095  0  661.3651 2 26825.334
    1500096  0 1060.0026 1  60813.33
    1500097  .         . 2  51146.09
    1500098  .         . 1  51146.09
    1500100  0   1615.66 2  43363.48
    1500101  0 1418.0198 1  43363.48
    1500103  3 1435.0386 1   73576.8
    1500105  .         0 1     21431
    1500106  0 105.06734 2     21431
    1500107  0 140.27469 1     21431
    1500108  0 260.66693 1 15757.143
    1500111  .         0 2 27531.305
    1500114  0  616.7527 1 71536.664
    1500115  0  1364.354 1  9333.333
    1500116  0   845.457 2  65547.86
    1500120  0  849.9174 1     42798
    1500121  1  694.4107 1  79206.66
    1500123  1  1598.022 2  87631.34
    1500126  0  919.6907 2     55508
    1500127  0 2722.1455 1  41018.33
    1500129  .         . 1     30267
    1500130  .         . 2     30267
    1500131  .         . 2 67339.336
    1500132  0 1638.4905 1   91041.2
    1500133  0    1577.7 2   91041.2
    1500134  0  661.7795 2     47628
    1500136  0  912.3063 2  50053.33
    1500137  1  691.1695 2     51034
    1500140  0 1219.2117 1  30307.31
    1500142  .         0 2   31309.5
    1500143  .         . 2   57014.5
    1500145  0 2075.8484 2     41572
    1500146  .         0 2   59285.5
    1500147  .         0 1   59285.5
    1500148  0  3977.003 2  121845.6
    1500149  0 1122.8436 2     33800
    1500150  0  957.3792 2     34002
    1500152  0 2998.9736 2     48864
    1500155  0  1948.939 2  63473.43
    1500159  .         . 2   58896.8
    1500160  .         . 2   58896.8
    1500161  .         . 1   58896.8
    1500163  0 1041.3129 2 36480.668
    1500166  .         0 2 73296.664
    1500168  0  530.2118 2 31999.445
    1500169  .         0 1  44980.29
    1500170  .         0 2  44980.29
    1500175  0 291.78607 1 34042.668
    1500177  0  736.3896 1     41518
    1500178  0  1946.247 1     84972
    1500181  0 566.26697 2     38094
    1500182  0  709.2154 2  56356.67
    1500184  .         0 1   39208.5
    1500185  0 2192.5264 1    138347
    1500186  0  1841.427 2    138347
    1500188  0  17918.41 1 68476.664
    1500189  .         0 1   54096.5
    1500190  .         0 1   54096.5
    1500191  0 152.17635 1 24402.666
    1500192  0 1345.4823 2     34191
    1500193  0  3569.598 2  113069.6
    1500195  .         . 2  52657.33
    1500196  .         . 2     99932
    1500198  0 1659.8325 1 13285.714
    1500199  .         0 2 13285.714
    1500201  0 2004.9705 2  48497.33
    1500202  .         . 2     50868
    1500203  0  530.0276 2  46667.33
    1500204  0 148.26433 1     30676
    1500208  .         0 2  7638.889
    1500210  0 1194.7814 1  96360.66
    1500211  0  495.9803 2     53865
    1500214  0  461.6954 2  51433.33
    1500217  0 2341.9646 1   38964.8
    1500218  0  704.5697 2     44603
    1500219  0  635.3337 2     44603
    1500221  0  4327.951 2 12666.667
    1500223  0 2030.2954 1     62950
    1500224  0 2087.5125 2     62950
    1500225  0 1558.2502 1     62950
    1500226  0  583.8546 1 65964.664
    1500227  .         . 2     78375
    1500230  0  1906.377 1     46094
    1500231  .         . 1     97661
    1500232  .         . 1 30560.666
    1500234  0 2144.5833 1 36821.332
    1500236  0 2281.3767 1  61991.33
    1500241  0   501.775 2   60688.5
    1500242  0  909.5589 1  49498.57
    1500243  0   856.088 2  49498.57
    1500244  0  877.5878 2  49498.57
    1500245  .         . 2     13276
    1500246  .         . 1     13276
    1500247  .         . 1     90156
    1500248  0 3387.8596 1  126306.4
    1500250  .         0 2  43162.38
    1500253  .         . 2    279449
    1500254  .         . 1    279449
    1500255  .         . 2    279449
    1500257  0  572.0884 2  49018.09
    1500261  0  795.1612 2     68888
    1500262  .         . 1     75276
    1500265  .         . 1   94914.2
    1500268  .         . 1 108807.78
    1500272  .         0 2  50244.78
    1500275  0 1511.9425 1  41306.19
    1500276  0 141.28271 2 18979.334
    1500278  0 1641.2476 2     35480
    1500280  .         . 1  22742.22
    1500282  0   903.976 2  97278.66
    1500284  0   1411.44 1     56247
    1500286  0  1225.103 2   60911.5
    1500287  0 1802.3956 2    118198
    1500289  0 3129.8506 1  43111.33
    1500290  1  495.4764 2 29400.666
    1500293  0  842.8392 1     58900
    1500294  0  964.5455 2    108602
    1500296  0  870.3943 2 70995.336
    1500298  3  724.2965 2   44676.4
    1500299  0  748.8852 1  48543.33
    1500300  0 538.77875 1 33445.715
    1500302  0 1172.0776 2  95058.66
    1500303  .         0 1  61375.77
    1500305  0    3396.4 2     27766
    1500306  .         . 1     27766
    1500307  .         . 2 28960.666
    1500308  .         0 1     74698
    1500311  .         . 1  52048.67
    1500313  0 1378.3098 2 108124.66
    1500315  0  714.8577 1 26568.666
    1500318  0 1334.6116 2    301610
    1500319  1  8893.913 2     27800
    1500320  .         . 1 72841.336
    1500321  2  1309.949 2   32530.5
    1500323  9 1864.9603 1     37426
    1500325  0 1696.5016 2     61830
    1500326  0 1260.3423 1 71605.336
    1500328  .         0 2 26741.334
    1500329  0  961.4438 2 77503.336
    1500330  0  2063.097 1  53430.67
    1500333  0         0 2     30888
    1500334  .         0 1  23659.13
    1500335  2  1348.435 2  23659.13
    1500337  0  1688.963 1     62170
    1500338  .         0 1  34405.24
    1500339  0   1663.13 2     46430
    1500340  0  1611.695 2     46430
    1500344  0  875.5526 1     41934
    1500346  0 525.39276 2 67377.336
    1500347  0 304.66434 1  55663.67
    end

    Many thanks,
    Ashani

  • #2
    conindex is from SSC (FAQ Advice #12). The command includes an option to save the data used to create the plots, but this applies only to the line data and does not include the confidence interval data.

    keepgraphdata(string) creates a new dataset storing the data used to graph the Lorenz or concentration curve, where string is the name of the dataset to be created.
    Here is a way to combine the graphs by directly extracting data from the plots. However, with overlapping confidence intervals, I’m not convinced it’s a good idea to display multiple graphs in a single plot. If you include more than two graphs, the result could be a confusing mess that your readers may find difficult to interpret.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long xwaveid float(pgsi scq_wgt sex hheqinc)
    1500001  .         . 2 36191.332
    1500002  .         . 2 36191.332
    1500003  .         . 1 36191.332
    1500004  . 1336.3737 1  43259.33
    1500006  .         0 2   44685.5
    1500007  .         . 2   44685.5
    1500010  .         . 1 126668.66
    1500012  0 1209.0953 2  45549.33
    1500018  0  897.1745 2     34340
    1500023  0 1879.3213 1 67506.664
    1500024  .         0 1  35680.77
    1500025  0 1954.5593 2  100449.6
    1500028  0 1095.2823 2  49080.67
    1500031  .         . 2     44462
    1500034  0 1225.8156 2  80826.66
    1500035  0  694.3892 1  46307.78
    1500038  5 1395.4056 1 15129.333
    1500039  0 1581.5642 1  57854.67
    1500042  0 1588.5952 1   33541.6
    1500045  0  1817.037 1  48392.14
    1500046  .         . 1  48392.14
    1500047  .         . 1 28703.334
    1500051  0   737.827 2     63013
    1500053  0  2861.205 1 27301.785
    1500054  0  4960.978 2 27301.785
    1500055  0  4686.794 2  48662.82
    1500056  0 4799.8564 1  48662.82
    1500060  0   8072.21 2  48662.82
    1500062  0  810.4601 2     35148
    1500064  .         0 2  30672.22
    1500066  0  161.7171 1 12819.333
    1500068  .         . 1   94625.5
    1500069  .         . 2   94625.5
    1500070  .         0 2   53920.5
    1500071  0 1117.5151 2  65297.33
    1500072  0  2202.835 1 29333.334
    1500073  0  762.3912 2 34087.332
    1500076  0 1028.1517 2  40757.08
    1500078  0   1418.98 1 24608.666
    1500082  0  495.1196 2  60330.67
    1500083  .         . 1  56118.23
    1500084  .         . 2  56118.23
    1500086  4 1142.2218 1 37424.668
    1500087  .         . 1     20885
    1500088  .         . 2     20885
    1500089  0  368.1432 1     42028
    1500090 11 1230.8286 1  40152.67
    1500091  .         . 1     96545
    1500092  0 10203.215 2 35772.855
    1500094  .         . 2  93092.66
    1500095  0  661.3651 2 26825.334
    1500096  0 1060.0026 1  60813.33
    1500097  .         . 2  51146.09
    1500098  .         . 1  51146.09
    1500100  0   1615.66 2  43363.48
    1500101  0 1418.0198 1  43363.48
    1500103  3 1435.0386 1   73576.8
    1500105  .         0 1     21431
    1500106  0 105.06734 2     21431
    1500107  0 140.27469 1     21431
    1500108  0 260.66693 1 15757.143
    1500111  .         0 2 27531.305
    1500114  0  616.7527 1 71536.664
    1500115  0  1364.354 1  9333.333
    1500116  0   845.457 2  65547.86
    1500120  0  849.9174 1     42798
    1500121  1  694.4107 1  79206.66
    1500123  1  1598.022 2  87631.34
    1500126  0  919.6907 2     55508
    1500127  0 2722.1455 1  41018.33
    1500129  .         . 1     30267
    1500130  .         . 2     30267
    1500131  .         . 2 67339.336
    1500132  0 1638.4905 1   91041.2
    1500133  0    1577.7 2   91041.2
    1500134  0  661.7795 2     47628
    1500136  0  912.3063 2  50053.33
    1500137  1  691.1695 2     51034
    1500140  0 1219.2117 1  30307.31
    1500142  .         0 2   31309.5
    1500143  .         . 2   57014.5
    1500145  0 2075.8484 2     41572
    1500146  .         0 2   59285.5
    1500147  .         0 1   59285.5
    1500148  0  3977.003 2  121845.6
    1500149  0 1122.8436 2     33800
    1500150  0  957.3792 2     34002
    1500152  0 2998.9736 2     48864
    1500155  0  1948.939 2  63473.43
    1500159  .         . 2   58896.8
    1500160  .         . 2   58896.8
    1500161  .         . 1   58896.8
    1500163  0 1041.3129 2 36480.668
    1500166  .         0 2 73296.664
    1500168  0  530.2118 2 31999.445
    1500169  .         0 1  44980.29
    1500170  .         0 2  44980.29
    1500175  0 291.78607 1 34042.668
    1500177  0  736.3896 1     41518
    1500178  0  1946.247 1     84972
    1500181  0 566.26697 2     38094
    1500182  0  709.2154 2  56356.67
    1500184  .         0 1   39208.5
    1500185  0 2192.5264 1    138347
    1500186  0  1841.427 2    138347
    1500188  0  17918.41 1 68476.664
    1500189  .         0 1   54096.5
    1500190  .         0 1   54096.5
    1500191  0 152.17635 1 24402.666
    1500192  0 1345.4823 2     34191
    1500193  0  3569.598 2  113069.6
    1500195  .         . 2  52657.33
    1500196  .         . 2     99932
    1500198  0 1659.8325 1 13285.714
    1500199  .         0 2 13285.714
    1500201  0 2004.9705 2  48497.33
    1500202  .         . 2     50868
    1500203  0  530.0276 2  46667.33
    1500204  0 148.26433 1     30676
    1500208  .         0 2  7638.889
    1500210  0 1194.7814 1  96360.66
    1500211  0  495.9803 2     53865
    1500214  0  461.6954 2  51433.33
    1500217  0 2341.9646 1   38964.8
    1500218  0  704.5697 2     44603
    1500219  0  635.3337 2     44603
    1500221  0  4327.951 2 12666.667
    1500223  0 2030.2954 1     62950
    1500224  0 2087.5125 2     62950
    1500225  0 1558.2502 1     62950
    1500226  0  583.8546 1 65964.664
    1500227  .         . 2     78375
    1500230  0  1906.377 1     46094
    1500231  .         . 1     97661
    1500232  .         . 1 30560.666
    1500234  0 2144.5833 1 36821.332
    1500236  0 2281.3767 1  61991.33
    1500241  0   501.775 2   60688.5
    1500242  0  909.5589 1  49498.57
    1500243  0   856.088 2  49498.57
    1500244  0  877.5878 2  49498.57
    1500245  .         . 2     13276
    1500246  .         . 1     13276
    1500247  .         . 1     90156
    1500248  0 3387.8596 1  126306.4
    1500250  .         0 2  43162.38
    1500253  .         . 2    279449
    1500254  .         . 1    279449
    1500255  .         . 2    279449
    1500257  0  572.0884 2  49018.09
    1500261  0  795.1612 2     68888
    1500262  .         . 1     75276
    1500265  .         . 1   94914.2
    1500268  .         . 1 108807.78
    1500272  .         0 2  50244.78
    1500275  0 1511.9425 1  41306.19
    1500276  0 141.28271 2 18979.334
    1500278  0 1641.2476 2     35480
    1500280  .         . 1  22742.22
    1500282  0   903.976 2  97278.66
    1500284  0   1411.44 1     56247
    1500286  0  1225.103 2   60911.5
    1500287  0 1802.3956 2    118198
    1500289  0 3129.8506 1  43111.33
    1500290  1  495.4764 2 29400.666
    1500293  0  842.8392 1     58900
    1500294  0  964.5455 2    108602
    1500296  0  870.3943 2 70995.336
    1500298  3  724.2965 2   44676.4
    1500299  0  748.8852 1  48543.33
    1500300  0 538.77875 1 33445.715
    1500302  0 1172.0776 2  95058.66
    1500303  .         0 1  61375.77
    1500305  0    3396.4 2     27766
    1500306  .         . 1     27766
    1500307  .         . 2 28960.666
    1500308  .         0 1     74698
    1500311  .         . 1  52048.67
    1500313  0 1378.3098 2 108124.66
    1500315  0  714.8577 1 26568.666
    1500318  0 1334.6116 2    301610
    1500319  1  8893.913 2     27800
    1500320  .         . 1 72841.336
    1500321  2  1309.949 2   32530.5
    1500323  9 1864.9603 1     37426
    1500325  0 1696.5016 2     61830
    1500326  0 1260.3423 1 71605.336
    1500328  .         0 2 26741.334
    1500329  0  961.4438 2 77503.336
    1500330  0  2063.097 1  53430.67
    1500333  0         0 2     30888
    1500334  .         0 1  23659.13
    1500335  2  1348.435 2  23659.13
    1500337  0  1688.963 1     62170
    1500338  .         0 1  34405.24
    1500339  0   1663.13 2     46430
    1500340  0  1611.695 2     46430
    1500344  0  875.5526 1     41934
    1500346  0 525.39276 2 67377.336
    1500347  0 304.66434 1  55663.67
    end
    
    
    conindex pgsi [pweight=scq_wgt] if sex==1, rankvar(hheqinc) robust cluster(xwaveid) truezero bounded limits(0 27)erreygers graph
    gr save gr1, replace
    
    conindex pgsi [pweight=scq_wgt] if sex==2, rankvar(hheqinc) robust cluster(xwaveid) truezero bounded limits(0 27)erreygers graph
    gr save gr2, replace
    
    tempfile graphdata
    forval s= 1/2{
        gr use gr`s'
        serset use, clear
        rename _* (cil ciu rank cshare)
        gen series=`s'
        cap append using `graphdata'
        save `graphdata', replace
    }
    
    use `graphdata', clear
    tw (rarea cil ciu rank if series==1, fcolor(blue%40) lc(blue%0)) ///
       (rarea cil ciu rank if series==2, fcolor(grey%40) lc(grey%0)) ///
       (line cshare rank if series==1, color(blue)) ///
       (line cshare rank if series==2, color(grey)), ///
       ytitle(Cumulative share of pgsi) xtitle(Rank of hheqinc) ///
       leg(order(1 "95% CI Female"  3 "L(p) Female"  2 "95% CI Male" 4 "L(p) Male"))
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	62.5 KB
ID:	1782412

    Last edited by Andrew Musau; 15 Oct 2025, 00:07.

    Comment


    • #3
      Thank you very much for this, much appreciated. Yes, I also realised it'll look too messy with the confidence intervals, so I might omit those and just show the curves.

      Comment


      • #4
        Hi Andrew, thanks again for this code which works for me. I have one more question - I need to add the 45 degree diagonal line of equality in the graph, how do I do this?

        Comment


        • #5
          I have one more question - I need to add the 45 degree diagonal line of equality in the graph, how do I do this?
          I don't think this makes any sense for this data. The cumulative share variable ranges from about 0 to 1, whereas the rank ranges from 0 to 100. How would the cumulative share be even close to the rank except perhaps at the first data point of the extreme left end of the graph? Am I missing something?

          In any case, the line of equality would not be anything like a 45 degree diagonal with this data. It would be a line at the left end of the graph whose slope is so large that it would appear to be completely vertical. It would probably be visually indistinguishable from the y-axis of your graph, or very nearly so.
          Last edited by Clyde Schechter; 16 Oct 2025, 17:43.

          Comment


          • #6
            Hi Clyde,

            Thank you for pointing that out and you are correct in that the axis values don't match for a line of equality. I think this has to do with this specific command and the way it graphs the data. For example, the individual graphs produced by the first two commands are two separate figures like the one below (smoother here than the sample data one since I'm using all the data), which despite the different scales produces a dashed diagonal line. I just want to retain this line when combining two or more curves in one graph.

            Code:
             
             conindex pgsi [pweight=scq_wgt] if sex==1, rankvar(hheqinc) robust cluster(xwaveid) truezero bounded limits(0 27)erreygers graph gr save gr1, replace  conindex pgsi [pweight=scq_wgt] if sex==2, rankvar(hheqinc) robust cluster(xwaveid) truezero bounded limits(0 27)erreygers graph gr save gr2, replace
            Click image for larger version

Name:	Graph.png
Views:	2
Size:	56.7 KB
ID:	1782463
            Attached Files

            Comment


            • #7
              Well, I see the diagonal line, but it is clearly not a line of inequality. The same mismatch in the range of values I pointed out in #5 obtains here as well. It seems to be a line like y = x/100. Assuming you want something like that, you can do this:
              Code:
              use `graphdata', clear
              gen rank100 = rank/100
              tw (rarea cil ciu rank if series==1, fcolor(blue%40) lc(blue%0)) ///
                 (rarea cil ciu rank if series==2, fcolor(grey%40) lc(grey%0)) ///
                 (line cshare rank if series==1, color(blue)) ///
                 (line cshare rank if series==2, color(grey)) ///
                 (line rank100 rank, color(black) lpattern(dash)), ///
                 ytitle(Cumulative share of pgsi) xtitle(Rank of hheqinc) ///
                 leg(order(1 "95% CI Female"  3 "L(p) Female"  2 "95% CI Male" 4 "L(p) Male"))

              Comment


              • #8
                See also https://journals.sagepub.com/doi/pdf...6867X241276116 for a miniature review of adding lines to graphs.

                twoway function
                and twoway scatteri are in my experience often neglected here.

                Comment


                • #9
                  Thank you very much Clyde for the code, and thank you Nick, I'll have a look at these options.

                  Comment

                  Working...
                  X