Announcement

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

  • Specific order descending in statplot

    Dear Stata-Listers,

    i created a statplot with a few 0 1 coded variables, showing the percentage of 1.
    I want to have a descending order, except having the Variable f13_SQ009_100 as last bar, is that possible?

    Here is my code:
    splitvarlabels f13_SQ001_100 - f13_SQ009_100 , length(40)

    statplot f13_SQ001_100 - f13_SQ009_100, varopts(relabel(`r(relabel)') sort(1) descending label(labsize(2.3))) ///
    ylabel( 0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%") ///
    blabel(bar, pos(inside) size(2.7) color(white) format(%2.0f)) ///
    yline(100, lstyle(grid) lwidth(.18) lcolor(gs8)) ytitle("") ///
    title("Welche dieser Tätigkeiten gehören zu Ihrem Aufgabenspektrum?", size(3.1)) ///
    note("Quelle: xxx, eigene Berechnungen, n= 180.", color("63 70 74")).






  • #2
    statplot is from SSC and I am one of the authors.

    splitvarlabels is some code posted on Statalist sometime.

    However, I am struggling to know what the problem is here without seeing either a graph or example data to run the code.

    Comment


    • #3
      Click image for larger version

Name:	Frage 1.3.png
Views:	1
Size:	74.7 KB
ID:	1715608



      Here is the graph, this is the descending order, i would like to have a descending order, but the Variable f13_SQ009_100 with the Label "Keine Tätigkeit mit Online-Bezug" should be as last


      Comment


      • #4
        The constraint that one variable must go last is easy to understand but there are no specific handles for it in statplot or indeed graph so far as I know.

        The easiest route is to create a small custom dataset of means in the desired sort order. Otherwise this may help.

        Code:
         
        webuse nlswork, clear 
        
        local vars msp nev_mar collgrad not_smsa c_city south union
        
        foreach v of local vars  { 
            local lbl`v' : var label `v'
            if "`lbl`v''" == "" local lbl`v' "`v'"
        }
        
        capture frame drop graphsandbox 
        
        frame put `vars', into(graphsandbox)
        
        frame graphsandbox { 
            collapse `vars'
            
            rename (`vars') (mean=) 
            gen id  = 1 
            reshape long mean, i(id) j(varname) string 
            
            gen lbl = "" 
            
            foreach v of local vars { 
                replace lbl = "`lbl`v''" if varname == "`v'"
            }
            
            gen last = varname == "nev_mar"
            gsort last -mean 
            replace mean = 100 * mean 
            
            gen order = _n 
            
            graph hbar (asis) mean, over(lbl, sort(order)) blabel(bar, format(%2.0f))  ysc(r(0 65) alt) ytitle(% saying Yes)
        }
        Click image for larger version

Name:	notastatplot.png
Views:	1
Size:	38.1 KB
ID:	1715613

        Comment


        • #5
          Thank u very much Nick. But now i cant use the splitvarlabels command right?

          Comment


          • #6
            Why not?

            As before if you want more focused advice then give a data example please. All that is needed is

            variable names

            variable labels

            means on each variable.

            The output from

            Code:
            describe f13_SQ001_100 - f13_SQ009_100 
            
            collapse f13_SQ001_100 - f13_SQ009_100
            
            dataex
            should suffice

            Comment


            • #7
              . describe f13_SQ001_100 - f13_SQ009_100

              Variable Storage Display Value
              name type format label Variable label
              --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
              f13_SQ001_100 float %9.0g Planung und Konzeption von Online-Angeboten
              f13_SQ002_100 float %9.0g Durchführung von Online-Kursangeboten (mehrere Termine in regelmäßigem Turnus)
              f13_SQ003_100 float %9.0g Durchführung von Online-Seminaren (einmaliger Termin)
              f13_SQ004_100 float %9.0g Durchführung/Moderation von Online-Infoveranstaltungen
              f13_SQ005_100 float %9.0g Erstellung von digitalen Schulungsinhalten (Erklärvideos, Tutorials)
              f13_SQ006_100 float %9.0g Erstellung von Online-Informationen (Blogs, Social Media-Posts, Webseite)
              f13_SQ007_100 float %9.0g Nutzung von Social Media zur Zielgruppenerreichung durch aktives Zugehen
              f13_SQ009_100 float %9.0g Keine Tätigkeit mit Online-Bezug

              .
              . collapse f13_SQ001_100 - f13_SQ009_100

              .
              . dataex

              ----------------------- copy starting from the next line -----------------------
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(f13_SQ001_100 f13_SQ002_100 f13_SQ003_100 f13_SQ004_100 f13_SQ005_100 f13_SQ006_100 f13_SQ007_100 f13_SQ009_100)
              38.54167 38.54167 26.041666 19.791666 10.416667 18.75 32.291668 46.66667
              end
              ------------------ copy up to and including the previous line ------------------

              Listed 1 out of 1 observations

              .
              .
              end of do-file

              Comment


              • #8
                sorry i forgot to say, these variables called
                f13_SQ001_100 and so on are already multiplied with 100

                Comment


                • #9
                  Nick i got, thank you very much, encoding the variable lbl was the solution!

                  Comment


                  • #10
                    Good -- I am going to take that as saying you solved your problem.

                    Comment


                    • #11
                      Yes, its solved, thank you! But i am still asking myself if it´s possible to do this without a local...

                      Comment


                      • #12
                        I don't feel any aversion to using local macros, but I have another and perhaps better answer.

                        The specific sort order wanted here is to order variables on their means, except that one particular variable must come last.

                        Let's invent and then solve a similar problem.

                        With the nlswork data we will look at the binary variables. Suppose for some reason you want to sort on their means, except that south should come last. The suggestion earlier in the thread was to get a dataset of means, and sort observations on that.

                        But we can just look at the means directly and work out a desired order.

                        If you want a more automated solution, a utility sortmean is part of the upsetplot package on SSC.

                        https://www.statalist.org/forums/for...lable-from-ssc

                        Its results can be copied or pasted, or can be picked up from a local macro (again).

                        In this example, long variable labels are not a problem, but if they were the solution you were using earlier still applies.

                        I've often seen problems in which a particular sort order is needed, except that some (often miscellaneous) category must come last, but such ad hoc wishes tend to get ad hoc solutions.

                        Code:
                        . webuse nlswork
                        (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
                        
                        
                        . su msp nev_mar collgrad not_smsa c_city south union
                        
                            Variable |        Obs        Mean    Std. dev.       Min        Max
                        -------------+---------------------------------------------------------
                                 msp |     28,518    .6029175    .4893019          0          1
                             nev_mar |     28,518    .2296795    .4206341          0          1
                            collgrad |     28,534    .1680451    .3739129          0          1
                            not_smsa |     28,526    .2824441    .4501961          0          1
                              c_city |     28,526     .357218    .4791882          0          1
                        -------------+---------------------------------------------------------
                               south |     28,526    .4095562    .4917605          0          1
                               union |     19,238    .2344319    .4236542          0          1
                        
                        . statplot msp c_city not_smsa union nev_mar collgrad south
                        
                        . sortmean msp nev_mar collgrad not_smsa c_city  union
                        nev_mar collgrad union not_smsa c_city msp
                        
                        . sortmean msp nev_mar collgrad not_smsa c_city  union, descending
                        msp c_city not_smsa union collgrad nev_mar
                        
                        . statplot msp c_city not_smsa union collgrad nev_mar south

                        Comment

                        Working...
                        X