Announcement

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

  • Putting order in catplot

    Hi

    I am going to put an order in this catplot, but it does not work. I mean, I need the graph appears in this order: first: 4 years & less, second: 6 years, third:9 years, forth:12 years, lastly: university.
    Could anyone please have a look at the codes?
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int ANO str14 Schooling
    2010 "6 Years"       
    2010 "12 Years"      
    2010 "6 Years"       
    2010 "9 Years"       
    2010 "9 Years"       
    2010 "University"    
    2010 "6 Years"       
    2010 "9 Years"       
    2010 "6 Years"       
    2010 "4 Years & Less"
    2010 "9 Years"       
    2010 "12 Years"      
    2010 "9 Years"       
    2010 "9 Years"       
    2010 "9 Years"       
    2010 "12 Years"      
    2010 "9 Years"       
    2010 "9 Years"       
    2010 "12 Years"      
    2010 "9 Years"       
    2011 "12 Years"      
    2011 "12 Years"      
    2011 "9 Years"       
    2011 "9 Years"       
    2011 "12 Years"      
    2011 "University"    
    2011 "9 Years"       
    2011 "12 Years"      
    2011 "9 Years"       
    2011 "9 Years"       
    2011 "12 Years"      
    2011 "12 Years"      
    2011 "12 Years"      
    2011 "12 Years"      
    2011 "9 Years"       
    2011 "6 Years"       
    2011 "University"    
    2011 "12 Years"      
    2011 "University"    
    2011 "University"    
    2012 "4 Years & Less"
    2012 "4 Years & Less"
    2012 "4 Years & Less"
    2012 "9 Years"       
    2012 "4 Years & Less"
    2012 "9 Years"       
    2012 "9 Years"       
    2012 "12 Years"      
    2012 "6 Years"       
    2012 "4 Years & Less"
    2012 "4 Years & Less"
    2012 "9 Years"       
    2012 "12 Years"      
    2012 "6 Years"       
    2012 "University"    
    2012 "University"    
    2012 "4 Years & Less"
    2012 "University"    
    2012 "12 Years"      
    2012 "13 Years"      
    2012 "14 Years"      
    end
    
    catplot Schooling ANO, percent(ANO) asyvars stack recast(bar) ///
    legend(order(4 years & less 6 years  9 years 12 years university )row(1) pos(6) size(small)) bar(1, fcolor(red*0.6) lcolor(red)) ///
    bar(2, fcolor(red*0.2) lcolor(red)) bar(3, fcolor(blue*0.2) lcolor(blue))  ///
    bar(4, fcolor(blue*0.6) lcolor(blue)) bar(5, fcolor(blue)) name(G2, replace) subtitle(% by ANO, place(w))

  • #2
    catplot is a community-contributed command by Nick Cox. Here's one approach using Nick's labmask command (although admittedly there are probably other ways of going about this).

    Code:
    gen id = cond(!mi(real(word(Schooling, 1))), real(word(Schooling, 1)), 15) 
    labmask id, val(Schooling)
    
    
    catplot id ANO, percent(ANO) asyvars stack recast(bar) ///
    legend(row(1) pos(6) size(small)) bar(1, fcolor(red*0.6) lcolor(red)) ///
    bar(2, fcolor(red*0.2) lcolor(red)) bar(3, fcolor(blue*0.2) lcolor(blue))  ///
    bar(4, fcolor(blue*0.6) lcolor(blue)) bar(5, fcolor(blue)) name(G2, replace) subtitle(% by ANO, place(w))

    Comment


    • #3
      Please follow FAQ Advice #12.

      If you are using community-contributed (also known as user-written) commands, explain that and say where they came from
      catplot is from SSC.

      Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
      The code you supplied to legend(order()) is such a mess that the code was ignored. That is an error message to be reported The numbers in order() must refer to the bars concerned, not the values of the variable. Text strings should be in " ".

      The underlying problem is that a string variable is only a good outcome to plot if its order does not matter or it is already ordered. That isn't true in your case. Best to grasp the nettle and push the variable through encode.


      This is better for your data example BUT

      1. It is likely that the full dataset has more categories.

      2. Even here there are 7 categories not 5.

      3. Other fixes are needed.



      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int ANO str14 Schooling
      2010 "6 Years"       
      2010 "12 Years"      
      2010 "6 Years"       
      2010 "9 Years"       
      2010 "9 Years"       
      2010 "University"    
      2010 "6 Years"       
      2010 "9 Years"       
      2010 "6 Years"       
      2010 "4 Years & Less"
      2010 "9 Years"       
      2010 "12 Years"      
      2010 "9 Years"       
      2010 "9 Years"       
      2010 "9 Years"       
      2010 "12 Years"      
      2010 "9 Years"       
      2010 "9 Years"       
      2010 "12 Years"      
      2010 "9 Years"       
      2011 "12 Years"      
      2011 "12 Years"      
      2011 "9 Years"       
      2011 "9 Years"       
      2011 "12 Years"      
      2011 "University"    
      2011 "9 Years"       
      2011 "12 Years"      
      2011 "9 Years"       
      2011 "9 Years"       
      2011 "12 Years"      
      2011 "12 Years"      
      2011 "12 Years"      
      2011 "12 Years"      
      2011 "9 Years"       
      2011 "6 Years"       
      2011 "University"    
      2011 "12 Years"      
      2011 "University"    
      2011 "University"    
      2012 "4 Years & Less"
      2012 "4 Years & Less"
      2012 "4 Years & Less"
      2012 "9 Years"       
      2012 "4 Years & Less"
      2012 "9 Years"       
      2012 "9 Years"       
      2012 "12 Years"      
      2012 "6 Years"       
      2012 "4 Years & Less"
      2012 "4 Years & Less"
      2012 "9 Years"       
      2012 "12 Years"      
      2012 "6 Years"       
      2012 "University"    
      2012 "University"    
      2012 "4 Years & Less"
      2012 "University"    
      2012 "12 Years"      
      2012 "13 Years"      
      2012 "14 Years"      
      end
      
      label def Education 1 "4 Years & Less" 2 "6 Years" 3 "9 Years" 4 "12 Years" 5 "13 Years"  6 "14 Years" 7 "University"
      encode Schooling, gen(Education) label(Education)
      
      catplot Education ANO, percent(ANO) asyvars stack recast(bar) ///
      legend(row(1) pos(6) size(vsmall)) bar(1, fcolor(red*0.6) lcolor(red)) ///
      bar(2, fcolor(red*0.2) lcolor(red)) bar(3, fcolor(blue*0.2) lcolor(blue))  ///
      bar(4, fcolor(blue*0.6) lcolor(blue)) bar(5, fcolor(blue)) name(G2, replace) subtitle(% by ANO, place(w))
      Click image for larger version

Name:	Paris_G2.png
Views:	1
Size:	29.0 KB
ID:	1699397

      Comment


      • #4
        Thank you so much. The result is what I need.

        Comment

        Working...
        X