Announcement

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

  • Graph bars by categories at different time points

    Hello!

    I struggle with grouping the bars by sex/gender. I have data (continuous) at 4 timepoints and a variable for sex (1=males 2=females). I want the bars to be groups by sex for each time point beside eachother but with a gap between the time points, i.e. mean for males and females at timepoint 1 (two bars beside each other) - gap - mean for males and females at timepoint 2 (two bars beside each other) - gap - and so on.

    The command "over" and "by" doesn't do this for me.

    Would appreciate your help a lot!

    Regards,

    Anders

  • #2
    Code:
    clear 
    set obs 80 
    set seed 2803 
    set scheme s1color 
    
    gen female = 1 + (_n > 40) 
    label def female 1 male 2 female 
    label val female female 
    
    egen time = seq(), block(5) to(4)
    
    gen whatever = rnormal(5, 1) + time + female 
    
    graph bar whatever, over(female) over(time) asyvars bar(1, col(blue)) bar(2, col(orange))

    Click image for larger version

Name:	whatever2.png
Views:	1
Size:	19.6 KB
ID:	1664555

    Comment


    • #3
      Thanks a lot, but unfortunately I lack the knowledge to apply your code on my variables.

      I have var1 (data at first follow-up (week 0) for males and females in together), var2 (data at second follow-up (week 16), var3 (data at third follow-up (week 52) and var4 (data at last follow-up (week 104). Then I have variable Sex (1= males, 2=females). I want to graph var1 to 4 in the same graph with mean of males in a bar beside the mean of females for var1 and then a gap and then the bars for mean of var2 for males and females and so on. It will be the bars for males and females beside each other at four different follow-ups (no need for any time scale on the x-axis) in the same graph.

      I do not actually understand how to apply your code to my case. Have tried but it doesn't turn out the way I wanted.

      Thanks a lot again för your kind help.

      Anders

      Comment


      • #4
        Being new to Stata is fine. The bigger problem here is being new to Statalist but ignoring advice to show precise detail: a data example, the exact code used and quite what went wrong. Most of the advice you need is at https://www.statalist.org/forums/help#stata as part of the FAQ Advice that is flagged as essential reading on every new message prompt.

        Holding data for different times in different variables is a wide layout and it's not so helpful in Stata as a long layout.

        Here is the structure I think you have and how to reshape long so that you can apply my code.

        Code:
         
        * you don't need to do this because it is just creating an example -- as you didn't give one. 
        clear 
        set obs 20 
        set seed 2803 
        set scheme s1color 
        
        gen Sex = 1 + (_n > 10) 
        label def female 1 male 2 female 
        label val Sex female 
        
        forval t = 1/4 { 
            gen var`t' = rnormal(5, 1) + `t' + Sex 
        }
        
        list 
        
             +----------------------------------------------------+
             |     Sex       var1       var2       var3       var4 |
             |----------------------------------------------------|
          1. |   male   6.535477   8.735983   8.381019   8.642193 |
          2. |   male   9.512294   7.711125   9.716956    8.24064 |
          3. |   male   7.650975   7.534252   7.518519   9.290953 |
          4. |   male   7.075804   9.284028   8.866832   11.77427 |
          5. |   male   7.144118   8.904225   8.921787   9.240471 |
             |----------------------------------------------------|
          6. |   male   7.432089   9.446353   8.774392   9.139339 |
          7. |   male   5.166383   7.288468   7.795892   9.260725 |
          8. |   male    6.73019   7.861185   9.411777   8.400434 |
          9. |   male   7.660946   7.912464   8.972856   10.04583 |
         10. |   male   8.519218   7.179851   7.619097   9.222196 |
             |----------------------------------------------------|
         11. | female   8.846945   8.958941   9.986059    9.20964 |
         12. | female    9.36384   9.224634   9.441957   12.78404 |
         13. | female   7.211944   8.665378   9.428722   12.11599 |
         14. | female   7.719409    8.38724    9.27916   11.12593 |
         15. | female   8.258056   7.367777   10.12364   12.55476 |
             |----------------------------------------------------|
         16. | female     9.8675   8.365119   10.70296   12.02539 |
         17. | female   9.659986   9.721516   9.911808   11.08846 |
         18. | female   5.907009   8.274912   9.782086    10.3299 |
         19. | female   6.942717   9.177871   9.514308   10.64533 |
         20. | female   9.086969   6.810063   10.23362   10.11272 |
             +----------------------------------------------------+
        
        . * you do need to do something like this to use my code; if you have an identifier variable you can use that instead 
        
        gen id = _n 
        reshape long var, i(id) j(time)
        
        graph bar var, over(Sex) over(time) asyvars bar(1, col(blue)) bar(2, col(orange))
        On the other hand, it is possible to get what you want without a reshape.

        You may have tried some variation on

        Code:
        .graph bar var?, over(Sex)  asyvars 
        .and got the bars in the wrong order so far as you are concerned. With statplot from SSC you can do this, but you must install the program before you can use it.
        .

        .
        Code:
        ssc install statplot 
        statplot var?, over(Sex) asyvars bar(1, col(blue)) bar(2, col(orange)) recast(bar)

        You surely need better variable names than var1 var2 var3 var4 or at least informative variable labels.

        Comment


        • #5
          Thank you for your reply. I apologise if you understood my reply as ignorant of your advice. I believe my knowledge in the graphical functions of STATA is to low to follow your code since I tried my best to replicate it without succeeding.

          My data looks like this:

          . list pid Sex SHS_sum_w0 SHS_sum_w16 SHS_sum_w52 SHS_sum_w104

          +---------------------------------------------------------+
          | pid Sex SHS_su~0 SHS_su~6 SHS_su~2 SHS_su~4 |
          |---------------------------------------------------------|
          1. | 13275 2 13 . . . |
          2. | 13325 2 12 . . . |
          3. | 13406 1 7 6 6 . |
          4. | 16116 2 7 . . . |
          5. | 16892 1 8 9 . . |
          |---------------------------------------------------------|
          6. | 17203 2 . 13 . . |
          7. | 17310 2 12 12 . . |
          8. | 17606 2 16 10 . . |
          9. | 17920 2 10 12 . . |
          10. | 19326 1 4 3 6 4 |
          |---------------------------------------------------------|
          11. | 19635 2 15 12 17 . |
          12. | 21592 2 17 16 10 2 |
          13. | 22944 2 9 . 8 5 |
          14. | 23473 1 8 6 4 4 |
          15. | 24271 1 7 5 6 9 |
          |---------------------------------------------------------|
          16. | 24272 2 14 . . . |
          17. | 24396 1 1 4 2 2 |
          18. | 25273 2 14 10 9 6 |
          19. | 27002 1 8 8 8 7 |
          20. | 27032 1 18 16 14 . |
          |---------------------------------------------------------|
          21. | 28027 2 4 6 . . |
          22. | 28066 1 9 . 1 4 |
          23. | 28366 1 6 8 4 5 |
          24. | 28370 1 7 7 . . |
          25. | 28402 1 6 5 9 . |
          |---------------------------------------------------------|
          26. | 28409 1 8 6 5 . |
          27. | 28452 1 9 4 1 0 |
          28. | 28454 2 6 2 4 . |
          29. | 28483 1 . 7 . . |
          30. | 28852 2 12 5 . . |
          |---------------------------------------------------------|
          31. | 29053 2 9 7 8 . |
          32. | 29451 1 3 3 2 4 |
          33. | 30349 1 10 7 4 1 |
          34. | 30484 1 5 5 . . |
          35. | 30513 2 6 9 3 13 |
          |---------------------------------------------------------|
          36. | 30534 1 11 5 3 0 |
          37. | 31059 1 8 6 8 . |
          38. | 31759 1 12 8 8 5 |
          39. | 31823 2 12 4 8 9 |
          40. | 32356 2 8 7 4 4 |
          |---------------------------------------------------------|
          41. | 32361 1 8 6 6 4 |
          42. | 32403 1 . 0 0 0 |
          43. | 32696 2 10 7 . . |
          44. | 32805 1 20 . 15 . |
          45. | 32878 1 11 11 5 4 |
          |---------------------------------------------------------|
          46. | 32939 1 10 . 5 0 |
          47. | 33717 1 8 2 2 1 |
          48. | 33782 1 13 10 . . |
          49. | 33817 2 9 8 3 5 |
          50. | 34167 1 10 4 . 3 |
          |---------------------------------------------------------|
          51. | 34375 2 16 8 13 8 |
          52. | 34819 1 6 . 4 4 |
          53. | 34847 1 4 5 . . |
          54. | 35528 2 7 9 9 7 |
          55. | 35676 2 7 9 4 4 |
          |---------------------------------------------------------|
          56. | 36090 2 11 9 10 11 |
          57. | 37230 1 12 1 . 4 |
          58. | 37233 2 3 . . . |
          59. | 37352 1 9 8 . . |
          60. | 37680 2 8 4 . . |
          |---------------------------------------------------------|
          61. | 41197 1 4 1 . . |
          62. | 41741 2 8 5 4 5 |
          63. | 41873 1 11 6 . 4 |
          64. | 42355 1 8 9 9 4 |
          65. | 42516 2 7 1 4 . |
          |---------------------------------------------------------|


          From this data I want to plot a bar graphs male/female (male=1, female=2) for each variable beside each other (grouping m/f for each variable). I will try to use your code and see if I succeed.

          Kind regards.

          Comment


          • #6
            The only new detail is that the variable names are not as you said in #3. Otherwise you've had all the hints already.


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte obsno long pid byte(Sex SHS_sum_w0 SHS_sum_w16 SHS_sum_w52 SHS_sum_w104)
             1 13275 2 13  .  .  .
             2 13325 2 12  .  .  .
             3 13406 1  7  6  6  .
             4 16116 2  7  .  .  .
             5 16892 1  8  9  .  .
             6 17203 2  . 13  .  .
             7 17310 2 12 12  .  .
             8 17606 2 16 10  .  .
             9 17920 2 10 12  .  .
            10 19326 1  4  3  6  4
            11 19635 2 15 12 17  .
            12 21592 2 17 16 10  2
            13 22944 2  9  .  8  5
            14 23473 1  8  6  4  4
            15 24271 1  7  5  6  9
            16 24272 2 14  .  .  .
            17 24396 1  1  4  2  2
            18 25273 2 14 10  9  6
            19 27002 1  8  8  8  7
            20 27032 1 18 16 14  .
            21 28027 2  4  6  .  .
            22 28066 1  9  .  1  4
            23 28366 1  6  8  4  5
            24 28370 1  7  7  .  .
            25 28402 1  6  5  9  .
            26 28409 1  8  6  5  .
            27 28452 1  9  4  1  0
            28 28454 2  6  2  4  .
            29 28483 1  .  7  .  .
            30 28852 2 12  5  .  .
            31 29053 2  9  7  8  .
            32 29451 1  3  3  2  4
            33 30349 1 10  7  4  1
            34 30484 1  5  5  .  .
            35 30513 2  6  9  3 13
            36 30534 1 11  5  3  0
            37 31059 1  8  6  8  .
            38 31759 1 12  8  8  5
            39 31823 2 12  4  8  9
            40 32356 2  8  7  4  4
            41 32361 1  8  6  6  4
            42 32403 1  .  0  0  0
            43 32696 2 10  7  .  .
            44 32805 1 20  . 15  .
            45 32878 1 11 11  5  4
            46 32939 1 10  .  5  0
            47 33717 1  8  2  2  1
            48 33782 1 13 10  .  .
            49 33817 2  9  8  3  5
            50 34167 1 10  4  .  3
            51 34375 2 16  8 13  8
            52 34819 1  6  .  4  4
            53 34847 1  4  5  .  .
            54 35528 2  7  9  9  7
            55 35676 2  7  9  4  4
            56 36090 2 11  9 10 11
            57 37230 1 12  1  .  4
            58 37233 2  3  .  .  .
            59 37352 1  9  8  .  .
            60 37680 2  8  4  .  .
            61 41197 1  4  1  .  .
            62 41741 2  8  5  4  5
            63 41873 1 11  6  .  4
            64 42355 1  8  9  9  4
            65 42516 2  7  1  4  .
            end
            
            * ssc install statplot 
            
            foreach v of var SHS_sum_* {
                local suffix : subinstr local v "SHS_sum_w" "", all 
                label var `v' "week `suffix'"
            } 
            
            label def Sex 1 male 2 female 
            label val Sex Sex 
            
            statplot  *w0 *w16 *w52 *w104, yla(, ang(h)) over(Sex) asyvars bar(1, col(blue)) bar(2, col(orange)) recast(bar) ytitle(mean SHS)
            I don't know what SHS is and therefore don't know units of measurement, if any.

            Click image for larger version

Name:	statplot..png
Views:	1
Size:	22.9 KB
ID:	1664732

            Comment


            • #7
              Thanks a lot for patience and help.

              Comment


              • #8
                On the other hand, showing just the means hides the variability. This design seems to reveal that multiples of 4 appear often. Scheduled Hospital Stay???

                The code isn't going to work well if the number of patients is really much larger than the data example.

                Click image for larger version

Name:	stripplotSHS.png
Views:	1
Size:	43.2 KB
ID:	1664854


                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input byte obsno long pid byte(Sex SHS_sum_w0 SHS_sum_w16 SHS_sum_w52 SHS_sum_w104)
                 1 13275 2 13  .  .  .
                 2 13325 2 12  .  .  .
                 3 13406 1  7  6  6  .
                 4 16116 2  7  .  .  .
                 5 16892 1  8  9  .  .
                 6 17203 2  . 13  .  .
                 7 17310 2 12 12  .  .
                 8 17606 2 16 10  .  .
                 9 17920 2 10 12  .  .
                10 19326 1  4  3  6  4
                11 19635 2 15 12 17  .
                12 21592 2 17 16 10  2
                13 22944 2  9  .  8  5
                14 23473 1  8  6  4  4
                15 24271 1  7  5  6  9
                16 24272 2 14  .  .  .
                17 24396 1  1  4  2  2
                18 25273 2 14 10  9  6
                19 27002 1  8  8  8  7
                20 27032 1 18 16 14  .
                21 28027 2  4  6  .  .
                22 28066 1  9  .  1  4
                23 28366 1  6  8  4  5
                24 28370 1  7  7  .  .
                25 28402 1  6  5  9  .
                26 28409 1  8  6  5  .
                27 28452 1  9  4  1  0
                28 28454 2  6  2  4  .
                29 28483 1  .  7  .  .
                30 28852 2 12  5  .  .
                31 29053 2  9  7  8  .
                32 29451 1  3  3  2  4
                33 30349 1 10  7  4  1
                34 30484 1  5  5  .  .
                35 30513 2  6  9  3 13
                36 30534 1 11  5  3  0
                37 31059 1  8  6  8  .
                38 31759 1 12  8  8  5
                39 31823 2 12  4  8  9
                40 32356 2  8  7  4  4
                41 32361 1  8  6  6  4
                42 32403 1  .  0  0  0
                43 32696 2 10  7  .  .
                44 32805 1 20  . 15  .
                45 32878 1 11 11  5  4
                46 32939 1 10  .  5  0
                47 33717 1  8  2  2  1
                48 33782 1 13 10  .  .
                49 33817 2  9  8  3  5
                50 34167 1 10  4  .  3
                51 34375 2 16  8 13  8
                52 34819 1  6  .  4  4
                53 34847 1  4  5  .  .
                54 35528 2  7  9  9  7
                55 35676 2  7  9  4  4
                56 36090 2 11  9 10 11
                57 37230 1 12  1  .  4
                58 37233 2  3  .  .  .
                59 37352 1  9  8  .  .
                60 37680 2  8  4  .  .
                61 41197 1  4  1  .  .
                62 41741 2  8  5  4  5
                63 41873 1 11  6  .  4
                64 42355 1  8  9  9  4
                65 42516 2  7  1  4  .
                end
                
                * ssc install statplot 
                
                foreach v of var SHS_sum_* {
                    local suffix : subinstr local v "SHS_sum_w" "", all 
                    label var `v' "week `suffix'"
                } 
                
                label def Sex 1 male 2 female 
                label val Sex Sex 
                
                reshape long SHS_sum_w, i(pid) j(time)
                
                * if stripplot not installed, uncomment the next comment 
                * ssc install stripplot 
                
                stripplot  SHS_sum_w, yla(0(4)20, ang(h)) vertical stack over(Sex) by(time, subtitle("Time in weeks") compact note("") row(1) legend(off))   separate(Sex) ms(oh sh) mc(blue orange) bar(mean(mc(black))) ytitle(SHS with mean)

                Comment

                Working...
                X