Announcement

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

  • bar order

    I would like to graph a bar chart where the bars are ordered in descending order, i.e. highest frequencies on the left, lowest frequencies to the right (sort of Pareto chart)
    ​Is it possibile? Thanks

  • #2
    Naturally. For example, graph bar has sort options.

    Comment


    • #3
      Thanks, but I cannot find it. My command is:

      graph bar (count), over( Physical_excercise )

      ​How do I add the sort option (descending bars) ?

      Comment


      • #4
        From the help:

        over_subopts Description
        ---------------------------------------------------------------------------
        relabel(# "text" ...) change axis labels
        label(cat_axis_label_options) rendition of labels
        axis(cat_axis_line_options) rendition of axis line

        gap([*]#) gap between bars within over() category
        sort(varname) put bars in prespecified order
        sort(#) put bars in height order
        sort((stat) varname) put bars in derived order
        descending reverse default or specified bar order
        reverse reverse scale to run from max to min
        ---------------------------------------------------------------------------

        Code:
        . sysuse auto
        (1978 Automobile Data)
        
        . graph bar (mean) mpg, over(rep78, sort(1))
        
        . graph bar (mean) mpg, over(rep78, sort(1) descending)

        Comment


        • #5
          it works! thanks

          Comment


          • #6
            Good afternoon. Please, I want the item "other" to go at the end
            Buenas tardes. Porfavor, quiero que el item "otro" vaya al final



            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float id byte item float p8_
            1  1 15.993628
            1  2  68.51059
            1  3  54.76839
            1  4 22.200207
            1  5 9.0018015
            1  6  8.100713
            1  7  20.40998
            1  8   2.77822
            1  9 22.006685
            1 10  1.452091
            1 11  3.620339
            end
            label values item item
            label def item 1 "No renovación de contratos", modify
            label def item 2 "Flexibilidad de horarios", modify
            label def item 3 "Reducción de horas trabajadas en la semana", modify
            label def item 4 "Vacaciones adelantadas", modify
            label def item 5 "Licencia sin goce de haber", modify
            label def item 6 "Suspensión perfecta de labores", modify
            label def item 7 "Reducción de remuneraciones", modify
            label def item 8 "Bono extra por trabajo presencial", modify
            label def item 9 "Subsidio a la planilla por Decreto de Urgencia", modify
            label def item 10 "Resolución de contratos (despido)", modify
            label def item 11 "Otro", modify
            
            
            splitvallabels item, length(100)
            #delimit;
            graph hbar (asis) p8_,
                  over(item ,descending label(labsize(*0.8) labgap(*1))
                  relabel(`r(relabel)') sort(1) gap(*1)) 
                  bar(1, color("0 32 96"))
                  nooutside
                  yscale(off)
                  ylabel(,nogrid) 
                  fysize(200) 
                  ytitle("", size(small)) 
                  title("" ,margin(l=0 r=0 t=3 b-5)) 
                  blabel(bar, format(%4,1f) color(navy))
                  intensity(25)
                  plotregion(style(none)) graphregion(margin(zero))
                  scheme(cleanplots)
                  play()
                  name(F6, replace)
                  scale(*1)
                  aspectratio(1);

            Comment


            • #7
              Install myaxis from the Stata Journal (also available from SSC)

              Code:
              ssc install myaxis
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float id byte item float p8_
              1  1 15.993628
              1  2  68.51059
              1  3  54.76839
              1  4 22.200207
              1  5 9.0018015
              1  6  8.100713
              1  7  20.40998
              1  8   2.77822
              1  9 22.006685
              1 10  1.452091
              1 11  3.620339
              end
              
              
              label values item item
              label def item 1 "No renovación de contratos", modify
              label def item 2 "Flexibilidad de horarios", modify
              label def item 3 "Reducción de horas trabajadas en la semana", modify
              label def item 4 "Vacaciones adelantadas", modify
              label def item 5 "Licencia sin goce de haber", modify
              label def item 6 "Suspensión perfecta de labores", modify
              label def item 7 "Reducción de remuneraciones", modify
              label def item 8 "Bono extra por trabajo presencial", modify
              label def item 9 "Subsidio a la planilla por Decreto de Urgencia", modify
              label def item 10 "Resolución de contratos (despido)", modify
              label def item 11 "Otro", modify
              
              gen p8_o= cond(item==11, 0, p8_)
              myaxis order=p8_, sort(total p8_o) descending
              
              splitvallabels item, length(100)
              #delimit;
              graph hbar (asis) p8_,
                    over(order , label(labsize(*0.8) labgap(*1))
                    relabel(`r(relabel)') gap(*1)) 
                    bar(1, color("0 32 96"))
                    nooutside
                    yscale(off)
                    ylabel(,nogrid) 
                    fysize(200) 
                    ytitle("", size(small)) 
                    title("" ,margin(l=0 r=0 t=3 b-5)) 
                    blabel(bar, format(%4,1f) color(navy))
                    intensity(25)
                    plotregion(style(none)) graphregion(margin(zero))
                    scheme(cleanplots)
                    play()
                    name(F6, replace)
                    scale(*1)
                    aspectratio(1);
              Click image for larger version

Name:	F6.png
Views:	1
Size:	34.1 KB
ID:	1658589

              Comment


              • #8
                thank you very much friend Andrew Musau

                Comment


                • #9
                  Greetings from Peru
                  Last edited by Andrés Lahur Talavera Cuya; 07 Apr 2022, 18:54.

                  Comment


                  • #10

                    To make a graph that fits the data, you must also add this change:

                    Code:
                    clear
                    input float id byte item float p8_
                    1  1 15.993628
                    1  2  68.51059
                    1  3  54.76839
                    1  4 22.200207
                    1  5 9.0018015
                    1  6  8.100713
                    1  7  20.40998
                    1  8   2.77822
                    1  9 22.006685
                    1 10  1.452091
                    1 11  3.620339
                    end
                    
                    
                    label values item item
                    label def item 1 "No renovación de contratos", modify
                    label def item 2 "Flexibilidad de horarios", modify
                    label def item 3 "Reducción de horas trabajadas en la semana", modify
                    label def item 4 "Vacaciones adelantadas", modify
                    label def item 5 "Licencia sin goce de haber", modify
                    label def item 6 "Suspensión perfecta de labores", modify
                    label def item 7 "Reducción de remuneraciones", modify
                    label def item 8 "Bono extra por trabajo presencial", modify
                    label def item 9 "Subsidio a la planilla por Decreto de Urgencia", modify
                    label def item 10 "Resolución de contratos (despido)", modify
                    label def item 11 "Otro", modify
                    
                    gen p8_o= cond(item==11, 0, p8_)
                    myaxis order=p8_, sort(total p8_o) descending
                    myaxis itemx=item, sort(total p8_o) descending
                    
                    splitvallabels itemx, length(100)
                    #delimit;
                    graph hbar (asis) p8_,
                          over(order , label(labsize(*0.8) labgap(*1))
                          relabel(`r(relabel)') gap(*1)) 
                          bar(1, color("0 32 96"))
                          nooutside
                          yscale(off)
                          ylabel(,nogrid) 
                          fysize(200) 
                          ytitle("", size(small)) 
                          title("" ,margin(l=0 r=0 t=3 b-5)) 
                          blabel(bar, format(%4,1f) color(navy))
                          intensity(25)
                          plotregion(style(none)) graphregion(margin(zero))
                          scheme(cleanplots)
                          play()
                          name(F6, replace)
                          scale(*1)
                          aspectratio(1);

                    Comment

                    Working...
                    X