Announcement

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

  • Graphs

    Hello,

    The variable 'var' in the below dataset has 10 categories for each state. I would like to have a bar (without space) for the value of each of the categories. I have three states in the dataset. On the x-axis, I would like to have all the categories for the first state together, then for the second together, and then for the third. How can I do that? Also, I would like to have a representation where each category is represented by a different color and then I can have a legend of what color represents which category.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte region_01 str6 var float abs_change_CHR
    4 "asst"    -29.45125
    4 "cfuel"   -24.19497
    4 "cm"     -2.3755288
    4 "edu"     -10.52449
    4 "elec"    -36.34635
    4 "hsg"     -24.19317
    4 "nutri"   -20.38776
    4 "sani"   -22.772703
    4 "satten"  -9.763616
    4 "wtr"     -13.75391
    5 "asst"    -37.14813
    5 "cfuel"  -24.682524
    5 "cm"      -3.596684
    5 "edu"    -15.092362
    5 "elec"   -34.890602
    5 "hsg"     -20.20026
    5 "nutri"  -22.283747
    5 "sani"    -23.79097
    5 "satten"  -31.06312
    5 "wtr"     -5.031923
    7 "asst"   -31.052834
    7 "cfuel"  -33.004265
    7 "cm"      -3.222605
    7 "edu"    -18.568455
    7 "elec"    -20.96565
    7 "hsg"    -31.243896
    7 "nutri"   -27.93447
    7 "sani"    -34.32783
    7 "satten" -15.453047
    7 "wtr"    -18.937517
    end
    label values region_01 reg
    label def reg 4 "Assam", modify
    label def reg 5 "Bihar", modify
    label def reg 7 "Chhattisgarh", modify

    Thank you.

  • #2
    Something like this?
    Code:
    graph bar (asis) abs_change_CHR, over(var) over(region_01, gap(*5)) asyvars scheme(s1color) intensity(*0.5) xalternate ytitle("Absolute Change") legend(rows(2))
    which produces:
    Click image for larger version

Name:	Screenshot 2022-10-28 at 12.39.53 AM.png
Views:	1
Size:	1.40 MB
ID:	1687054

    Comment


    • #3
      Thank you so much. I was able to plot the one in which all the categories are represented by just one color, but I wanted exactly like the one you have drawn. Thank you, again.

      Comment


      • #4
        Although Varsha got exactly what they wanted, I suggest that there are much better graph types that should be tried. I find this graph colourful but ineffective.

        Alphabetical order of categories is good for graphs that will serve as dictionaries or directories -- the main role is to look up particular categories -- but not otherwise.

        A legend is at best a necessary evil. Using this one imposes a great burden on readers, who need a great deal of back and forth between legend and graph if they are conscientious about trying to understand the fine structure. Many will think "nice colourful graph" and move on, or rely wholly on the commentary you add. Is the graph then more help than a table?

        It's hard to see patterns in comparing states.

        Here are two candidates. There are many others. I can't supply better text for the very cryptic category labels, but with these designs there is space to make them longer. There should be a precise explanation of what kind of change (is it really "absolute"? what are the units and time frame?).

        Some people might want to swap axes.

        I've chosen colours for each state to contrast clearly, but there might be better choices. Perhaps the states have official colours, or there are better choices on other grounds. Evidently the state names allow a specific set of marker labels that should be easy for readers to remember.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte region_01 str6 var float abs_change_CHR
        4 "asst"    -29.45125
        4 "cfuel"   -24.19497
        4 "cm"     -2.3755288
        4 "edu"     -10.52449
        4 "elec"    -36.34635
        4 "hsg"     -24.19317
        4 "nutri"   -20.38776
        4 "sani"   -22.772703
        4 "satten"  -9.763616
        4 "wtr"     -13.75391
        5 "asst"    -37.14813
        5 "cfuel"  -24.682524
        5 "cm"      -3.596684
        5 "edu"    -15.092362
        5 "elec"   -34.890602
        5 "hsg"     -20.20026
        5 "nutri"  -22.283747
        5 "sani"    -23.79097
        5 "satten"  -31.06312
        5 "wtr"     -5.031923
        7 "asst"   -31.052834
        7 "cfuel"  -33.004265
        7 "cm"      -3.222605
        7 "edu"    -18.568455
        7 "elec"    -20.96565
        7 "hsg"    -31.243896
        7 "nutri"   -27.93447
        7 "sani"    -34.32783
        7 "satten" -15.453047
        7 "wtr"    -18.937517
        end
        label values region_01 reg
        label def reg 4 "Assam", modify
        label def reg 5 "Bihar", modify
        label def reg 7 "Chhattisgarh", modify
        
        rename abs_change_CHR change
        
        * myaxis is from the Stata Journal
        myaxis better=var, sort(median change)
        drop var
        
        reshape wide change, i(better) j(region_01)
        
        foreach v in A B C {
            gen `v' = "`v'"
        }
        
        local opts ms(none) mlabpos(0) mlabsize(medium)
        
        scatter better change4, mlabel(A) mlabc(black) `opts' ///
        || scatter better change5, mlabel(B) mlabc(red) `opts' ///
        || scatter better change7, mlabel(C) mlabc(blue) `opts' ///
        yla(1/10, valuelabel ang(h) noticks grid glc(gs12)) xsc(alt) legend(off) b2title("A = Assam, B = Bihar, C = Chhattisgarh") ytitle("???") xtitle(change) name(G1, replace)
        
        twoway connected better change4, mlabel(A) mlabc(black) lc(black)`opts' ///
        || connected better change5, mlabel(B) mlabc(red) lc(red) `opts' ///
        || connected better change7, mlabel(C) mlabc(blue) lc(blue) `opts' ///
        yla(1/10, valuelabel ang(h) noticks grid glc(gs12)) xsc(alt) legend(off) b2title("A = Assam, B = Bihar, C = Chhattisgarh") ytitle("???") xtitle(change) name(G2, replace)
        Click image for larger version

Name:	varsha_G1.png
Views:	1
Size:	30.2 KB
ID:	1687097

        Click image for larger version

Name:	varsha_G2.png
Views:	1
Size:	49.5 KB
ID:	1687098

        Last edited by Nick Cox; 28 Oct 2022, 02:50.

        Comment


        • #5
          Here is code for swapped axes, I would say it works about as well, provided you can bear the cryptic category labels, which might be very clear in context.

          Code:
          local opts ms(none ..) mlabpos(0 0 0) mlabsize(medium medium medium) yla(, ang(h)) xsc(alt)
          
          scatter change? better, mlabel(A B C) mlabc(black red blue) `opts' ///
          xla(1/10, valuelabel ang(h) noticks grid glc(gs12)) legend(off) b2title("A = Assam, B = Bihar, C = Chhattisgarh") xtitle("???") ytitle(change) name(G3, replace)
          
          twoway connected change? better, mlabel(A B C) mlabc(black red blue) lc(black red blue) `opts' ///
          xla(1/10, valuelabel ang(h) noticks grid glc(gs12)) legend(off) b2title("A = Assam, B = Bihar, C = Chhattisgarh") xtitle("???") ytitle(change) name(G4, replace)
          Click image for larger version

Name:	varsha_G3.png
Views:	1
Size:	34.9 KB
ID:	1687100

          Click image for larger version

Name:	varsha_G4.png
Views:	1
Size:	55.7 KB
ID:	1687101




          Whenever a graph has table flavour, which is often, putting an x axis at the top is a possible style choice. More at https://www.stata-journal.com/articl...article=gr0053

          I agree with those who dislike connections because the axis in question is really categorical. The argument for connection can only be psychological, that they help grasp patterns for each state as a whole. There are many such applications under headings such as profile plots or parallel coordinates.
          Last edited by Nick Cox; 28 Oct 2022, 03:17.

          Comment


          • #6
            Thank you Nick for these other ways of representing the data.

            Comment

            Working...
            X