Announcement

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

  • How to plot proportion category data of a variable for different years (eg: past and recent years) in Stata


    Hi,
    How to plot the proportion categories of a variable in Stata for the data for 27 districts for two years. Please help with this. Thank you
    district year p1_hort p2_pul p3_cer p4_oil p5_comm
    Bagalkot 2001 0.045079 0.08583 0.53261 0.18189 0.154591
    Bangalore Rural 2001 0.286939 0.109285 0.595931 0.007667 0.000178
    Bangalore Urban 2001 0.255538 0.069513 0.468274 0.192811 0.013865
    Belgaum 2001 0.058672 0.07757 0.486318 0.144917 0.232522
    Bellary 2001 0.071131 0.082896 0.489547 0.243906 0.112519
    Bidar 2001 0.021251 0.537259 0.286827 0.085646 0.069018
    Bijapur 2001 0.067705 0.106219 0.515165 0.270293 0.040618
    CHAMARAJANAGAR 2001 0.105601 0.214008 0.432284 0.103957 0.14415
    Chikkamagalur 2001 0.326751 0.102199 0.497086 0.060053 0.013911
    Chitradurga 2001 0.209277 0.072619 0.33055 0.350101 0.037453
    DAKSHINA KANNADA 2001 0.581829 0.021473 0.391436 0.002872 0.002391
    DAVANAGERE 2001 0.117541 0.047116 0.690144 0.071016 0.074182
    Dharwad 2001 0.243137 0.14034 0.31116 0.077869 0.227494
    Gadag 2001 0.107744 0.16519 0.291153 0.25717 0.178744
    Gulbarga 2001 0.016718 0.468815 0.37184 0.115506 0.02712
    Hassan 2001 0.314212 0.141458 0.47757 0.04294 0.02382
    Haveri 2001 0.179243 0.073369 0.483934 0.071889 0.191566
    Kodagu 2001 0.418268 0.013313 0.56839 2.89E-05 0
    Kolar 2001 0.336459 0.070466 0.418053 0.171426 0.003596
    Koppal 2001 0.029403 0.146538 0.451724 0.286497 0.085839
    Mandya 2001 0.198702 0.154804 0.494253 0.037313 0.114928
    Mysore 2001 0.074135 0.241603 0.494822 0.04218 0.14726
    Raichur 2001 0.028646 0.085532 0.576559 0.236553 0.07271
    Shimoga 2001 0.197671 0.02244 0.68321 0.012366 0.084312
    Tumkur 2001 0.242021 0.123642 0.388874 0.241134 0.004329
    UDUPI 2001 0.426847 0.075604 0.472374 0.01847 0.006705
    UTTARA KANNADA 2001 0.21702 0.018386 0.681127 0.026336 0.057131
    district year p1_hort p2_pul p3_cer p4_oil p5_comm
    Bagalkot 2016 0.088936 0.292822 0.348233 0.109488 0.160521
    Bangalore Rural 2016 0.349698 0.113841 0.512517 0.022864 0.001081
    Bangalore Urban 2016 0.32624 0.082227 0.579051 0.012259 0.000222
    Belgaum 2016 0.076029 0.154616 0.401394 0.133162 0.234799
    Bellary 2016 0.16873 0.055851 0.532399 0.114508 0.128512
    Bidar 2016 0.043857 0.445756 0.124455 0.344521 0.041411
    Bijapur 2016 0.03859 0.590399 0.245534 0.06869 0.056787
    CHAMARAJANAGAR 2016 0.333899 0.209429 0.265142 0.113749 0.077781
    Chikkamagalur 2016 0.467009 0.067515 0.403381 0.049267 0.012828
    Chitradurga 2016 0.294915 0.06916 0.348091 0.270425 0.017409
    DAKSHINA KANNADA 2016 0.741826 0.00839 0.247704 0.002017 6.3E-05
    DAVANAGERE 2016 0.209308 0.04034 0.677895 0.036796 0.035661
    Dharwad 2016 0.17432 0.228438 0.278038 0.160898 0.158306
    Gadag 2016 0.105831 0.383137 0.26369 0.143622 0.10372
    Gulbarga 2016 0.021103 0.555648 0.265566 0.07714 0.080542
    Hassan 2016 0.388619 0.054398 0.540903 0.009691 0.00639
    Haveri 2016 0.17222 0.024165 0.544357 0.072161 0.187097
    Kodagu 2016 0.531987 0.00022 0.46697 0.000823 0
    Kolar 2016 0.396115 0.097136 0.4121 0.093906 0.000743
    Koppal 2016 0.082129 0.261504 0.456939 0.18819 0.011239
    Mandya 2016 0.351581 0.17175 0.365983 0.013852 0.096835
    Mysore 2016 0.200706 0.218343 0.420292 0.017242 0.143418
    Raichur 2016 0.027855 0.353385 0.424605 0.113972 0.080184
    Shimoga 2016 0.339281 0.005114 0.637924 0.001753 0.015927
    Tumkur 2016 0.412748 0.069122 0.333102 0.181434 0.003594
    UDUPI 2016 0.515739 0.031189 0.436897 0.015869 0.000307
    UTTARA KANNADA 2016 0.327405 0.012352 0.586567 0.013991 0.059685

  • #2
    That's quite a challenge and there are many ways to do this badly with 27 districts x 5 types x 2 years!

    Here's some technique. I've made various guesses with varying degrees of confidence.

    1. You really want the names shown clearly, so for example scatter plots and line charts are likely to be less effective.

    2. Alphabetical order is unlikely to be informative.

    3. There is no good reason why some names are SHOUTED OUT IN ALL CAPS, but ignore the fix here if there is.

    4. Change, if any, is of central interest. I've ordered each type according to value in 2016, but other possibilities include value in 2001, (change from 2001 to 2016), and so on.

    5. The most likely conventional solutions, 27 x 2 stacked bar charts or pie charts, are doomed to failure.

    6. Something cleverer, such as a two-dimensional reduction from a principal component analysis or correspondence analysis, isn't ruled out but not the first thing to try.

    labmask here is from the Stata Journal.

    Code:
    . search labmask, sj
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    SJ-8-2  gr0034  . . . . . . . . . .  Speaking Stata: Between tables and graphs
            (help labmask, seqvar if installed) . . . . . . . . . . . .  N. J. Cox
            Q2/08   SJ 8(2):269--289
            outlines techniques for producing table-like graphs
    Using arrows to show change was also discussed there.

    Code:
    . search arrows, sj
    
    <other hits> 
    
    SJ-5-2  gr0015  . . . . . . . . Stata tip 21: The arrows of outrageous fortune
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            Q2/05   SJ 5(2):282--284                                 (no commands)
            tip for using graph twoway pcarrow for graphing changes
            over time
    Putting the horizontal axis at the top with anything with table flavour is a matter of taste, but discussed there also

    Code:
    SJ-12-3 gr0053  . Speaking Stata: Axis practice, or what goes where on a graph
            (help multqplot if installed) . . . . . . . . . . . . . . .  N. J. Cox
            Q3/12   SJ 12(3):549--561
            discusses variations on what goes on each axis of a two-way
            plot; provides multiple quantile plots


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 district str4 year str8(p1_hort p2_pul p3_cer p4_oil p5_comm)
    "Bagalkot"         "2001" "0.045079" "0.08583"  "0.53261"  "0.18189"  "0.154591"
    "Bangalore Rural"  "2001" "0.286939" "0.109285" "0.595931" "0.007667" "0.000178"
    "Bangalore Urban"  "2001" "0.255538" "0.069513" "0.468274" "0.192811" "0.013865"
    "Belgaum"          "2001" "0.058672" "0.07757"  "0.486318" "0.144917" "0.232522"
    "Bellary"          "2001" "0.071131" "0.082896" "0.489547" "0.243906" "0.112519"
    "Bidar"            "2001" "0.021251" "0.537259" "0.286827" "0.085646" "0.069018"
    "Bijapur"          "2001" "0.067705" "0.106219" "0.515165" "0.270293" "0.040618"
    "CHAMARAJANAGAR"   "2001" "0.105601" "0.214008" "0.432284" "0.103957" "0.14415" 
    "Chikkamagalur"    "2001" "0.326751" "0.102199" "0.497086" "0.060053" "0.013911"
    "Chitradurga"      "2001" "0.209277" "0.072619" "0.33055"  "0.350101" "0.037453"
    "DAKSHINA KANNADA" "2001" "0.581829" "0.021473" "0.391436" "0.002872" "0.002391"
    "DAVANAGERE"       "2001" "0.117541" "0.047116" "0.690144" "0.071016" "0.074182"
    "Dharwad"          "2001" "0.243137" "0.14034"  "0.31116"  "0.077869" "0.227494"
    "Gadag"            "2001" "0.107744" "0.16519"  "0.291153" "0.25717"  "0.178744"
    "Gulbarga"         "2001" "0.016718" "0.468815" "0.37184"  "0.115506" "0.02712" 
    "Hassan"           "2001" "0.314212" "0.141458" "0.47757"  "0.04294"  "0.02382" 
    "Haveri"           "2001" "0.179243" "0.073369" "0.483934" "0.071889" "0.191566"
    "Kodagu"           "2001" "0.418268" "0.013313" "0.56839"  "2.89E-05" "0"       
    "Kolar"            "2001" "0.336459" "0.070466" "0.418053" "0.171426" "0.003596"
    "Koppal"           "2001" "0.029403" "0.146538" "0.451724" "0.286497" "0.085839"
    "Mandya"           "2001" "0.198702" "0.154804" "0.494253" "0.037313" "0.114928"
    "Mysore"           "2001" "0.074135" "0.241603" "0.494822" "0.04218"  "0.14726" 
    "Raichur"          "2001" "0.028646" "0.085532" "0.576559" "0.236553" "0.07271" 
    "Shimoga"          "2001" "0.197671" "0.02244"  "0.68321"  "0.012366" "0.084312"
    "Tumkur"           "2001" "0.242021" "0.123642" "0.388874" "0.241134" "0.004329"
    "UDUPI"            "2001" "0.426847" "0.075604" "0.472374" "0.01847"  "0.006705"
    "UTTARA KANNADA"   "2001" "0.21702"  "0.018386" "0.681127" "0.026336" "0.057131"
    "district"         "year" "p1_hort"  "p2_pul"   "p3_cer"   "p4_oil"   "p5_comm" 
    "Bagalkot"         "2016" "0.088936" "0.292822" "0.348233" "0.109488" "0.160521"
    "Bangalore Rural"  "2016" "0.349698" "0.113841" "0.512517" "0.022864" "0.001081"
    "Bangalore Urban"  "2016" "0.32624"  "0.082227" "0.579051" "0.012259" "0.000222"
    "Belgaum"          "2016" "0.076029" "0.154616" "0.401394" "0.133162" "0.234799"
    "Bellary"          "2016" "0.16873"  "0.055851" "0.532399" "0.114508" "0.128512"
    "Bidar"            "2016" "0.043857" "0.445756" "0.124455" "0.344521" "0.041411"
    "Bijapur"          "2016" "0.03859"  "0.590399" "0.245534" "0.06869"  "0.056787"
    "CHAMARAJANAGAR"   "2016" "0.333899" "0.209429" "0.265142" "0.113749" "0.077781"
    "Chikkamagalur"    "2016" "0.467009" "0.067515" "0.403381" "0.049267" "0.012828"
    "Chitradurga"      "2016" "0.294915" "0.06916"  "0.348091" "0.270425" "0.017409"
    "DAKSHINA KANNADA" "2016" "0.741826" "0.00839"  "0.247704" "0.002017" "6.3E-05" 
    "DAVANAGERE"       "2016" "0.209308" "0.04034"  "0.677895" "0.036796" "0.035661"
    "Dharwad"          "2016" "0.17432"  "0.228438" "0.278038" "0.160898" "0.158306"
    "Gadag"            "2016" "0.105831" "0.383137" "0.26369"  "0.143622" "0.10372" 
    "Gulbarga"         "2016" "0.021103" "0.555648" "0.265566" "0.07714"  "0.080542"
    "Hassan"           "2016" "0.388619" "0.054398" "0.540903" "0.009691" "0.00639" 
    "Haveri"           "2016" "0.17222"  "0.024165" "0.544357" "0.072161" "0.187097"
    "Kodagu"           "2016" "0.531987" "0.00022"  "0.46697"  "0.000823" "0"       
    "Kolar"            "2016" "0.396115" "0.097136" "0.4121"   "0.093906" "0.000743"
    "Koppal"           "2016" "0.082129" "0.261504" "0.456939" "0.18819"  "0.011239"
    "Mandya"           "2016" "0.351581" "0.17175"  "0.365983" "0.013852" "0.096835"
    "Mysore"           "2016" "0.200706" "0.218343" "0.420292" "0.017242" "0.143418"
    "Raichur"          "2016" "0.027855" "0.353385" "0.424605" "0.113972" "0.080184"
    "Shimoga"          "2016" "0.339281" "0.005114" "0.637924" "0.001753" "0.015927"
    "Tumkur"           "2016" "0.412748" "0.069122" "0.333102" "0.181434" "0.003594"
    "UDUPI"            "2016" "0.515739" "0.031189" "0.436897" "0.015869" "0.000307"
    "UTTARA KANNADA"   "2016" "0.327405" "0.012352" "0.586567" "0.013991" "0.059685"
    end
    
    drop if district == "district"
    replace district = proper(district)
    destring, replace 
    
    foreach v of var p* { 
        local new = substr("`v'", 4, .)
        rename `v' `new' 
        replace `new' = 100 * `new'
        local stubs `stubs' `new'
    }
    
    reshape wide `stubs', i(district) j(year)
    
    foreach s of local stubs { 
        egen rank = rank(`s'2016), unique 
        labmask rank, values(district)
        twoway pcarrow `s'2001 rank `s'2016 rank, horizontal yla(1/27, noticks valuelabel) name(`s', replace) /// 
        xsc(alt) xtitle(`s' 2001 to 2016 (%)) ytitle("")
        drop rank 
    }
    Here is a sample result. You need better titles for these graphs: Horticulture? Pulses? ???
    Click image for larger version

Name:	hort.png
Views:	1
Size:	85.0 KB
ID:	1729189


    Comment


    • #3
      A refinement on #2 would to colour positive and negative changes differently. Here is some code where different.

      Code:
      foreach s of local stubs { 
          egen rank = rank(`s'2016), unique 
          labmask rank, values(district)
          twoway pcarrow `s'2001 rank `s'2016 rank if `s'2016 >= `s'2001, horizontal yla(1/27, noticks valuelabel) name(`s', replace) /// 
          xsc(alt) xtitle(`s' 2001 to 2016 (%)) ytitle("") || ///
          pcarrow `s'2001 rank `s'2016 rank if `s'2016 <`s'2001, horizontal legend(off)
          drop rank 
      }

      Comment


      • #4
        Originally posted by Nick Cox View Post
        A refinement on #2 would to colour positive and negative changes differently. Here is some code where different.

        Code:
        foreach s of local stubs {
        egen rank = rank(`s'2016), unique
        labmask rank, values(district)
        twoway pcarrow `s'2001 rank `s'2016 rank if `s'2016 >= `s'2001, horizontal yla(1/27, noticks valuelabel) name(`s', replace) ///
        xsc(alt) xtitle(`s' 2001 to 2016 (%)) ytitle("") || ///
        pcarrow `s'2001 rank `s'2016 rank if `s'2016 <`s'2001, horizontal legend(off)
        drop rank
        }
        Thank you, Professor Nick Cox, for your kind reply to my query.

        It helps a lot. I am using Stata 14. First, I tried with a line graph after generating the mean of all the districts, where I am not able to present my data at the individual district level, which is my study objective.

        Sorry that I have not detailed my data, and you rightly guessed.

        The above result graph is perfect as it has both the names of the districts with the change from 2001 to 2016, and I would like to generate the same type of results for the five crop categories of horticulture, pulses, oilseeds, cereals, and commercial crops.

        I have installed labmask and arrows and tried to run the codes suggested by you. I am not able to view the graphs.

        After inputting the data, I run the below codes given by you. But no graph is generated. What is the error here?





        . drop if district == "district"
        (1 observation deleted)

        .
        . replace district = proper(district)
        (10 real changes made)

        .
        . destring, replace
        district contains nonnumeric characters; no replace
        year has all characters numeric; replaced as int
        p1_hort has all characters numeric; replaced as double
        p2_pul has all characters numeric; replaced as double
        p3_cer has all characters numeric; replaced as double
        p4_oil has all characters numeric; replaced as double
        p5_comm has all characters numeric; replaced as double

        .
        .
        .
        . foreach v of var p* {
        2.
        . local new = substr("`v'", 4, .)
        3.
        . rename `v' `new'
        4.
        . replace `new' = 100 * `new'
        5.
        . local stubs `stubs' `new'
        6. }
        (54 real changes made)
        (54 real changes made)
        (54 real changes made)
        (54 real changes made)
        (52 real changes made)

        .
        .
        .
        . reshape wide `stubs', i(district) j(year)
        (note: j = 2001 2016)

        Data long -> wide
        -----------------------------------------------------------------------------
        Number of obs. 54 -> 27
        Number of variables 7 -> 11
        j variable (2 values) year -> (dropped)
        xij variables:
        hort -> hort2001 hort2016
        pul -> pul2001 pul2016
        cer -> cer2001 cer2016
        oil -> oil2001 oil2016
        comm -> comm2001 comm2016
        -----------------------------------------------------------------------------

        .
        .
        .
        . foreach s of local stubs {
        2.
        . egen rank = rank(`s'2016), unique
        3.
        . labmask rank, values(district)
        4.
        . twoway pcarrow `s'2001 rank `s'2016 rank, horizontal yla(1/27, noticks valuelabel) name(`s', replace) ///
        5.
        . xsc(alt) xtitle(`s' 2001 to 2016 (%)) ytitle("")
        6.
        . drop rank
        7.
        . }
        option / not allowed
        r(198);


        Please help with this. Thank you

        Radhika

        Comment


        • #5
          My guess: You're trying to use /// comments interactively by typing commands one by one in the Command window.

          /// comments are only allowed in do-files and programs.

          You could just omit /// and type one long command, but better practice is to treat code as an entire script to be copied into, and executed from, the do-file editor.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            My guess: You're trying to use /// comments interactively by typing commands one by one in the Command window.

            /// comments are only allowed in do-files and programs.

            You could just omit /// and type one long command, but better practice is to treat code as an entire script to be copied into and executed from the do-file editor.
            Dear Professor,

            Thank you for your kind reply. I got the results when I tried it with the do file editor. I am new to Stata software and thought of copying the codes directly.

            I could edit the results using the graph editor tool and change the titles accordingly.

            I am very much grateful to you for your kind help.

            Thanks and regards

            Radhika



            Comment

            Working...
            X