Announcement

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

  • How to do a histogram? - survey

    Hello,

    I have an example of the dataset below (survey). I would like to do a histogram with the answers (inapplicable, refusal, don´t know, yes, no) of the variable "i_aidhh" by sex (variable i_sex). Can you help me with this?
    Thank you in advance.


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(pidp pid) byte i_sex int i_dvage byte i_aidhh
       76165  10689869 2 34  2
      732365  15752658 1 32  1
     1587125  17870879 2 51  1
     4849085 176725733 1 34  2
    68002725  10023526 2 63 -8
    68008847        -8 2 59 -8
    68010887        -8 2 53  2
    68029931        -8 1 48  2
    68031967        -8 2 69 -8
    68035365  10403086 1 65 -8
    68035367        -8 1 36  2
    68041487        -8 2 47  2
    68041491        -8 1 44  2
    68045567        -8 2 55  2
    68051007        -8 1 56  2
    68051011        -8 2 49  2
    68058487        -8 1 77  2
    68058491        -8 2 68  2
    68060531        -8 2 44  2
    68060533 160066204 2 61  1
    68060537 160066239 1 73  2
    68063247        -8 2 50  2
    68063927        -8 2 47  2
    68063931        -8 1 49  2
    68064605  10653872 1 68  2
    68064609  10653902 2 65  2
    68068007        -8 1 50  2
    68068082        -8 1 56  2
    68097245  10913629 2 67 -8
    68097927        -8 2 68 -8
    68120367        -8 2 66 -8
    68120375        -8 2 37 -8
    68125127        -8 2 51  2
    68125131        -8 1 26  2
    68125135        -8 2 21  2
    68133285  11218193 2 68 -8
    68133289  11218282 2 32  2
    68136009  11234989 2 65  2
    68137365  11240547 2 63 -8
    68138045  11242787 1 68  2
    68138049  11242817 2 67  1
    68138051        -8 2 63  2
    68144847        -8 1 49  2
    68144851        -8 2 41  2
    68148247        -8 1 69  2
    68150971        -8 2 58  2
    68150975        -8 1 29  2
    68155047        -8 2 60  2
    68155051        -8 1 65  2
    68157771        -8 2 23  2
    68159131        -8 2 37  1
    68160485  11418567 2 66  2
    68160489  11418591 1 41 -8
    68173407        -8 2 60  2
    68180887        -8 2 47  2
    68184971        -8 2 42  2
    68185647        -8 2 55  2
    68187687        -8 1 62  2
    68187691        -8 2 58  2
    68191771        -8 2 45  2
    68193127        -8 2 64  2
    68195167        -8 1 74  2
    68195171        -8 2 74  2
    68195851        -8 2 43  2
    68197887        -8 2 56  2
    68197899        -8 2 22  2
    68197903        -8 1 19  2
    68199247        -8 1 33  2
    68207407        -8 2 72  2
    68207411        -8 1 78  2
    68211487        -8 1 65 -8
    68214207        -8 1 58 -8
    68216247        -8 2 43  2
    68218287        -8 1 74  2
    68231223        -8 2 18  2
    68238011        -8 2 59  2
    68262487        -8 1 47  2
    68266567        -8 2 80 -8
    68278127        -8 2 70 -8
    68288327        -8 2 44  2
    68288331        -8 1 44  2
    68291731        -8 2 62 -8
    68293087        -8 2 49  2
    68293091        -8 1 54  2
    68293095        -8 1 29  2
    68293099        -8 1 26  2
    68293168        -8 1 28  2
    68294447        -8 1 60  2
    68294451        -8 2 44  2
    68297845  12521361 1 63  2
    68297849  12521396 2 61  2
    68297857  12521469 2 30  2
    68299207        -8 2 60  2
    68302611        -8 2 74  2
    68309407        -8 2 62  2
    68321647        -8 1 60  2
    68321651        -8 2 60  2
    68322327        -8 2 50  2
    68322331        -8 1 57  2
    68329807        -8 2 51  2
    end
    label values pid pid
    label def pid -8 "inapplicable", modify
    label values i_sex i_sex
    label def i_sex 1 "male", modify
    label def i_sex 2 "female", modify
    label values i_dvage i_dvage
    label values i_aidhh i_aidhh
    label def i_aidhh -8 "inapplicable", modify
    label def i_aidhh 1 "Yes", modify
    label def i_aidhh 2 "No", modify
    Best regards

  • #2
    What commands did you try? Do you want frequencies, proportions (of what? each sex, or total) or percents (of what? ditto) Horizontal or vertical? Colours? Numbers on graph? Adjacent bars or bars separated by spaces?


    You could play with

    Code:
    histogram 
    
    graph bar 
    
    graph hbar
    and so forth.

    Comment


    • #3
      Dear Nick C0x,

      I would like to have a histogram of the proportions of male and female (i_sex) that answer to the question given by "i_aidhh" (answers:inapplicable, refusal, don´t know, yes, no). Vertical and blue bars with separed spaces. I tried to use the graph hbar withou success.
      Thank you in advance,

      Comment


      • #4
        catplot from SSC can be used as a wrapper for graph bar. Note that graph hbar gives you horizontal bars.

        Code:
        catplot i_aidhh i_sex , fraction(i_sex) bar(1, color(blue)) recast(bar) yla(, ang(h)) ytitle(proportion)
        is token code.


        Click image for larger version

Name:	vasconcelos.png
Views:	1
Size:	17.7 KB
ID:	1639576

        Comment


        • #5
          Thank you very much Nick Cox!
          Best regards

          Comment


          • #6
            Hello,

            Is it possible to use the command catplot and performe several graphs at the same time (all the graphs in the same picture). For example, a picture like the one above but for proporti9ons of men and women that answer questions represented by i_aidhh, cg_scghqa, cg_scghqd, cg_scghqe.
            I send an example of the dataset below.
            Thank you in advance


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input long(pidp pid) byte i_sex int i_dvage byte(i_aidhh cg_scghqa cg_scghqd cg_scghqe)
             885066255        -8 1 17  2  2  2  2
            1224120379        -8 2 16  2  2  2  2
            1429168935        -8 2 16  2  3  3  3
            1361071019        -8 1 16  2  3  2  2
             272167291        -8 1 16  2 -8 -8 -8
            1292417539        -8 2 17  2  2  2  1
            1564188375        -8 2 17  2  3  2  2
             409305619        -8 2 16  2  3  2  2
             408257737 110343158 2 16  2  2  2  2
            1293309015        -8 2 17  2  2  2  1
            1021652415        -8 2 16  2  2  2  2
             682492227        -8 1 16  2  1  1  1
            1497033611        -8 2 17  2  2  2  2
             748175463        -8 2 16  2  3  4  4
             884242099        -8 2 16  2  3  2  3
             820043297 111214416 2 16  2  2  2  2
             816301935        -8 2 16  2  2  2  2
             749776179        -8 2 16  2  3  2  2
            1496920063        -8 2 16  2  2  2  2
            1565561979        -8 2 16  2  2  2  2
            1031845850        -8 1 16  2  3  2  3
             546315417 112553435 2 16  2  3  2  3
            1428843899        -8 2 17  2  4  2  2
             612092499        -8 1 16  2  2  2  3
            1632393059        -8 2 16  2  1  2  2
             682408579        -8 1 16  2  2  2  1
             953001659        -8 2 16  2  4  3  3
             701148013 118047434 2 16  2  2  2  3
            1158289575        -8 1 16  2  2  2  2
            1225137655        -8 2 16  2  2  2  2
             818669693 124195539 2 16  2  2  2  3
            1293130179        -8 1 17  2  1  1  1
            1632393055        -8 2 16  2  3  2  2
             818423537 112571875 2 16  2  3  3  2
             632753613 117671118 2 16  2  2  2  2
            1089077815        -8 2 16  2  3  4  3
            1428017015        -8 2 17  2  4  3  4
            1156923455        -8 2 16  2  3  3  3
            1089013903        -8 2 17  2  3  2  3
             274023701 113393075 2 16  2  3  3  2
            1565173695        -8 2 16  2  2  2  3
            1156289695        -8 1 17  2  2  2  1
            1443368130        -8 1 17  2  2  2  2
             682428973 123427789 1 16  2  2  2  3
            1578286930        -8 1 16  2  2  2  1
            1496734419        -8 1 17  2  4  4  4
             683337457 113050585 2 16  2  2  3  2
            1293054011        -8 2 17  2  1  2  3
             612794935        -8 2 16  2  2  2  1
              89129665 107106965 2 16  2  2  2  2
            1293998535        -8 1 16  2  4  2  2
            1022188935        -8 2 21  2  3  3  3
             952731695        -8 2 21 -8  4  4  4
             820046009  73312118 1 20  2  1  1  1
             272023815        -8 2 18  2  2  2  2
             490280693 105302333 2 17  2  3  3  3
             750116173 113721722 1 16  2 -8 -8 -8
             136195855        -8 2 18  2 -8 -8 -8
            1020476703        -8 1 21  2  1  2  2
             748607255        -8 1 20  2  2  2  1
            1168097250        -8 1 22 -8 -2 -2 -2
             544735783        -8 2 18  2  2  2  2
             748996899        -8 2 21  2  2  2  2
             545089383        -8 2 20  2  2  3  2
             273068971        -8 2 21  2  3  2  2
             884805819        -8 2 17  2  3  2  3
            1497727223        -8 2 18  1  3  2  1
            1175386930        -8 1 17  2  2  2  2
             275096733  76132579 2 19  2  2  2  1
             478611895        -8 2 21  2  2  2  2
             476520899        -8 2 17  2  3  3  4
            1088523615        -8 2 18  2  2  2  1
             544788815        -8 2 17  2 -8 -8 -8
            1293178459        -8 2 21 -8  2  1  2
            1033260130        -8 1 19  2  2  3  3
            1434943654        -8 1 20  2  2  1  3
             478131135        -8 1 18  2  4  2  4
            1632415495        -8 1 22 -8  3  2  2
            1026412572        -8 1 21  2  1  1  2
             544785419        -8 1 20  2  2  3  2
            1565094815        -8 2 22  2  2  2  1
             681024095        -8 2 20  2 -8 -8 -8
             613106379        -8 1 21  2  2  2  2
            1224479415        -8 1 17  2  2  2  2
             137612971        -8 2 19  2  2  2  2
            1564894215        -8 2 19  2  2  3  3
            1156363815        -8 1 22  2  2  2  2
             820121574        -8 2 19  2  2  2  2
             503461821 118966758 1 20  2  2  2  1
            1224956775        -8 2 21  2  3  3  2
             680277455        -8 2 18  2  2  3  2
             207123937  86305107 2 20  2  2  2  1
             816492335        -8 2 18  2  3  3  3
            1497004379        -8 2 20  2  2  2  2
             748552863        -8 1 17  2  2  2  2
             272537223        -8 1 19  2  2  2  2
            1428728295        -8 2 19 -8  2  1  1
             136886063        -8 2 16  2  2  2  3
             204017695        -8 1 19  2  2  1  3
            1225749655        -8 1 20  2  2  2  1
            end
            label values pid pid
            label def pid -8 "inapplicable", modify
            label values i_sex i_sex
            label def i_sex 1 "male", modify
            label def i_sex 2 "female", modify
            label values i_dvage i_dvage
            label values i_aidhh i_aidhh
            label def i_aidhh -8 "inapplicable", modify
            label def i_aidhh 1 "Yes", modify
            label def i_aidhh 2 "No", modify
            label values cg_scghqa cg_scghqa
            label def cg_scghqa -8 "inapplicable", modify
            label def cg_scghqa -2 "refusal", modify
            label def cg_scghqa 1 "Better than usual", modify
            label def cg_scghqa 2 "Same as usual", modify
            label def cg_scghqa 3 "Less than usual", modify
            label def cg_scghqa 4 "Much less than usual", modify
            label values cg_scghqd cg_scghqd
            label def cg_scghqd -8 "inapplicable", modify
            label def cg_scghqd -2 "refusal", modify
            label def cg_scghqd 1 "More so than usual", modify
            label def cg_scghqd 2 "Same as usual", modify
            label def cg_scghqd 3 "Less so than usual", modify
            label def cg_scghqd 4 "Much less capable", modify
            label values cg_scghqe cg_scghqe
            label def cg_scghqe -8 "inapplicable", modify
            label def cg_scghqe -2 "refusal", modify
            label def cg_scghqe 1 "Not at all", modify
            label def cg_scghqe 2 "No more than usual", modify
            label def cg_scghqe 3 "Rather more than usual", modify
            label def cg_scghqe 4 "Much more than usual", modify

            Comment


            • #7
              Sample code:

              Code:
              set scheme s1color
              local j = 0
              local graphs
              foreach v in i_aidhh cg_scghqa cg_scghqd cg_scghqe {
                  local ++j
                  catplot `v' if `v' > 0, name(G`j', replace)
                  local graphs `graphs' G`j'
              }
              
              graph combine `graphs'
              Comparing https://www.statalist.org/forums/for...s-same-picture you may naturally use code like

              Code:
              catplot `v' i_sex if `v' > 0, name(G`j', replace)
              Last edited by Nick Cox; 24 Jan 2022, 17:34.

              Comment


              • #8
                Thank you very much Nick Cox. This is what I was looking for.

                Comment

                Working...
                X