Announcement

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

  • histogram over age groups?

    Dear All, I find this question here. The data set is:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(age cognlab)
    48 3
    50 2
    59 2
    62 2
    50 2
    48 2
    63 2
    55 1
    69 2
    70 3
    83 2
    81 1
    83 1
    58 1
    61 2
    47 3
    47 3
    59 1
    67 1
    59 2
    53 2
    56 2
    51 2
    49 2
    57 1
    64 2
    52 1
    47 1
    55 2
    59 2
    59 2
    68 2
    56 1
    53 1
    59 1
    55 2
    48 1
    47 3
    46 1
    43 2
    47 1
    49 1
    66 2
    46 2
    45 2
    63 1
    62 1
    47 3
    57 1
    64 2
    58 1
    72 1
    56 2
    61 1
    45 2
    41 3
    54 2
    56 2
    67 1
    59 1
    59 2
    48 1
    52 2
    49 2
    56 2
    73 2
    70 1
    61 2
    80 1
    76 1
    63 1
    50 2
    58 2
    50 3
    52 2
    66 1
    59 2
    60 1
    54 2
    55 2
    48 2
    75 2
    66 2
    59 2
    58 2
    74 1
    62 2
    60 2
    64 2
    67 1
    51 2
    51 2
    53 2
    70 1
    70 2
    61 2
    61 2
    67 1
    61 1
    56 1
    end
    1. The end is to draw a graph like below:
      Click image for larger version

Name:	100543nxetq78dez22qut4.png
Views:	1
Size:	49.3 KB
ID:	1477071
    2. Age in on the horizontal axis, with three groups. The first group is 45-60, the second group is 60-75, and the third group is age > 75.
    3. The variable on the vertical axis is `cognlab', which has only three values, i.e., 1,2, and 3.
    4. Any suggestions are appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I could not read the link, for it is not in English. That said, it seems you need to deal with stacked bar graphs.
    Best regards,

    Marcos

    Comment


    • #3
      Dear Marcos, The original question is in Chinese. I guess so too (stacked bar) but the problem is how?
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        This would often be called a stacked (or subdivided) bar chart. You could do it with graph bar, although for this particular application I am more familiar with catplot (SSC). I would suggest that tabplot (Stata Journal) is also competitive here.

        Code:
        gen age2 = 15 * floor(age/15)
        foreach age in 30 45 60 75 {
            local AGE = `age' + 14
            label define age2 `age' "`age'-`AGE'", modify
        }
        label val age2 age2
        
        catplot cognlab age2, percent(age2) stack recast(bar) asyvars yla(, ang(h)) ///
        legend(order(3 2 1) pos(3) col(1)) ///
        bar(3, bcolor(blue*0.6)) ///
        bar(2, fcolor(blue*0.2) lcolor(blue*0.6)) ///
        bar(1, fcolor(red*0.2) lcolor(red*0.6)) b1title(Age (years)) name(G1, replace)
        
        tabplot cognlab age2, percent(age2) yla(, ang(h)) ///
        showval ///
        xtitle(Age (years)) name(G2, replace) ///
        subtitle(% in age group) ///
        yreverse ///
        separate(cognlab) ///
        bar3(bcolor(blue*0.6)) ///
        bar2(bfcolor(blue*0.2) blcolor(blue*0.6)) ///
        bar1(bfcolor(red*0.2) blcolor(red*0.6))
        Click image for larger version

Name:	huang_1.png
Views:	1
Size:	32.0 KB
ID:	1477080

        Click image for larger version

Name:	huang_2.png
Views:	1
Size:	29.0 KB
ID:	1477081

        Comment


        • #5
          River Huang My comment in #2 was basically to confirm that you wanted the stacked bar graphs, as I guessed, but not some histograms, as stated in the title.

          Being this so, in spite of Nick having already given a couple of great strategies, I just wish to share this text since it shows how to produce such graphs with Stata's ordinary commands.

          Hopefully it helps.
          Last edited by Marcos Almeida; 04 Jan 2019, 04:54.
          Best regards,

          Marcos

          Comment


          • #6
            The age binning is avoidable. This works intelligibly with the sample given, but not so well with much larger samples.

            Code:
            * ssc inst stripplot 
            stripplot age, over(cognlab) stack height(0.4) ms(Sh)
            Click image for larger version

Name:	huang_3.png
Views:	1
Size:	29.0 KB
ID:	1477121

            Comment


            • #7
              Dear Marcos, Thanks for the suggestion. I will pass this information to the questioner.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment


              • #8
                Dear Nick, Thanks for the suggestion. I will pass this information to the questioner.
                Ho-Chuan (River) Huang
                Stata 19.0, MP(4)

                Comment


                • #9
                  Dear Nick, I tried your code and found the following message
                  Code:
                  . tabplot cognlab age2, percent(age2) yla(, ang(h)) ///
                  > showval ///
                  > xtitle(Age (years)) name(G2, replace) ///
                  > subtitle(% in age group) ///
                  > yreverse ///
                  > separate(cognlab) ///
                  > bar3(bcolor(blue*0.6)) ///
                  > bar2(bfcolor(blue*0.2) blcolor(blue*0.6)) ///
                  > bar1(bfcolor(red*0.2) blcolor(red*0.6))
                  option yreverse not allowed
                  r(198);
                  Any idea?
                  Ho-Chuan (River) Huang
                  Stata 19.0, MP(4)

                  Comment


                  • #10
                    So, you are not using the latest version. Pay attention, 007. https://www.imdb.com/title/tt0879840/

                    Code:
                    search tabplot


                    SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
                    (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
                    Q3/17 SJ 17(3):779
                    added options for reversing axis scales; improved handling of
                    axis labels containing quotation marks

                    SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
                    (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
                    Q2/16 SJ 16(2):491--510
                    provides multiple bar charts in table form representing
                    contingency tables for one, two, or three categorical variables

                    SJ-4-2 gr0004 . Speaking Stata: Graphing categorical and compositional data
                    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
                    Q2/04 SJ 4(2):190--215 (no commands)
                    discusses graphical possibilities for categorical and
                    compositional data

                    Comment


                    • #11
                      Dear Nick, I have updated the -tabplot- package as follows.
                      Click image for larger version

Name:	tabplot.png
Views:	1
Size:	9.1 KB
ID:	1477245

                      However, same error message appeared.
                      Code:
                      . gen age2 = 15 * floor(age/15)
                      
                      . foreach age in 30 45 60 75 {
                        2.     local AGE = `age' + 14
                        3.     label define age2 `age' "`age'-`AGE'", modify
                        4. }
                      
                      . label val age2 age2
                      
                      . 
                      . tabplot cognlab age2, percent(age2) yla(, ang(h)) ///
                      > showval ///
                      > xtitle(Age (years)) name(G2, replace) ///
                      > subtitle(% in age group) ///
                      > yreverse ///
                      > separate(cognlab) ///
                      > bar3(bcolor(blue*0.6)) ///
                      > bar2(bfcolor(blue*0.2) blcolor(blue*0.6)) ///
                      > bar1(bfcolor(red*0.2) blcolor(red*0.6))
                      option yreverse not allowed
                      r(198);
                      Ho-Chuan (River) Huang
                      Stata 19.0, MP(4)

                      Comment


                      • #12
                        Presumably, the old program code is still in memory.

                        Code:
                        discard
                        or

                        Code:
                        program drop tabplot

                        Comment


                        • #13
                          Dear Nick, Unfortunately, it doesn't work, either. (same error message!) I uninstall tabplot and re-install tabplot, but with no luck.
                          Ho-Chuan (River) Huang
                          Stata 19.0, MP(4)

                          Comment


                          • #14
                            I can't easily help further. The code worked for me -- otherwise #4 was impossible -- and the version on my machine is what you can see by

                            Code:
                            type http://www.stata-journal.com/software/sj17-3/gr0066_1/tabplot.ado
                            You need to check

                            Code:
                            which tabplot, all
                            as somehow you have left an older version for Stata to use.

                            Comment


                            • #15
                              Also, as said, update from the Stata Journal ​​​​​​, not from SSC.

                              Comment

                              Working...
                              X