Announcement

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

  • putting multiple graphs for quantitative vs categorical variables in one graph

    Hi,

    I have 7 variables, which are actually for one survey question that asks respondents to rank the importance of 7 options. The data were entered in a way that separates the seven choices as 7 variables and each row was a ranking for the separate variables.

    the data looks like this:
    variable name: health repro breed meat nutr care rankoth
    variable label: Animal Health and Well Being Reproduction Animal Breeding/Genetics Meat Science Nutrition Management and daily care of animals Others
    ID: 1 3 1 2 6 4 5 7
    ID: 2 3 1 2 7 5 4 6
    That way, I need all seven variables on one graph for it to make sense. For instance, the simplest graph I want is just displaying the means of these 7 variables on one graph, probably as a bar chart.
    Further analysis would require a side-by-side bar chart that displays the mean ranking (y) for each of the seven variables (x) by gender (so there are two bars).

    In short, I just want to put multiple 2-way graphs between "1 categorical variable and (mean of) 1 quantitative variable" of multiple quantitative variables on one graph.

    This is something I want:
    Click image for larger version

Name:	Picture1.png
Views:	1
Size:	22.7 KB
ID:	1545704



    is there any way to do that in stata? Thanks!
    Last edited by Ashley Liu; 09 Apr 2020, 03:12.

  • #2
    Please read and act on https://www.statalist.org/forums/help#stata in presenting data examples. Yours required a lot of surgery and a new variable to get to this:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id health repro breed meat nutr care rankoth) float girl
    1 3 1 2 6 4 5 7 0
    2 3 1 2 7 5 4 6 1
    end
    label values girl girl
    label def girl 0 "boy", modify
    label def girl 1 "girl", modify

    statplot from SSC can help here.

    Code:
    ssc install statplot
    Code:
    statplot health-rankoth , over(girl) recast(bar) varnames

    Comment


    • #3
      Dear Nick, Following your example above, is it possible to have different colors for boy/girl? Thanks.
      Click image for larger version

Name:	boy-girl.png
Views:	1
Size:	62.0 KB
ID:	1550351

      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        The usual way to do this in graph bar works.

        Code:
        statplot ... ,asyvars showyvars leg(off)

        Comment


        • #5
          Dear Andrew, Many thanks for this helpful suggestion.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Dear Andrew, By the way, I have this dataset and code
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(lly btot privo) byte income4
             41.0236  83.9456  65.4788 1
             66.9684  95.7224 57.85528 2
              78.022  90.3459 55.01267 2
             74.4912    92.72 62.01511 3
             53.3381  74.3563 36.72324 3
            125.9392  96.7181 128.3783 3
             70.3053  92.7402 65.04914 3
             54.7421  94.7271 49.14149 3
             51.9463  84.2815 37.43197 3
            77.47569  87.7728 59.08989 3
             31.7611  88.9397 34.79329 3
             67.4966  98.4359 65.29842 4
              45.349  97.2227 51.78182 4
             49.0197  92.0092 25.64779 4
             54.0413  90.0224 81.62302 4
               63.37  96.5411 75.46823 4
            71.41078  98.0962 86.69409 4
            57.46272   97.571 76.45579 4
             49.4787  88.0999 42.44699 4
             48.6267  83.5528 46.31212 4
            56.50429   88.997 60.86423 4
             53.4937  88.9401 89.11234 4
             51.7266  92.6655 54.81786 4
              49.626    82.43 37.59236 4
            123.4055  98.9879 141.2944 4
             62.1195  93.1142 113.0658 4
            143.4259   92.565 43.97008 2
             63.7392  96.4534 46.89995 2
             19.1631  61.6758 21.44617 2
             51.4366  94.7736 71.93586 3
             51.5927  91.8502 40.64384 3
             25.5744  69.3824 22.89418 3
             46.8707  82.2124 24.36066 3
             22.9597  52.8085  27.8104 3
             29.4668  59.2383 21.21116 3
             18.3381  74.1596 15.68565 3
             37.4615  91.7183 31.39167 4
             36.8425  90.8998 33.12155 4
             47.7859  84.6563 47.27738 1
             27.5044  81.4047 26.97613 1
             16.3877  31.5974 13.14186 2
             17.6203  65.2917 14.51989 2
             20.5826  73.3352 19.11292 2
             31.0542   89.117  20.8444 2
             30.3415  57.7968 16.20979 2
             26.9358  72.0441 22.84953 2
             20.0486  62.1616 17.98507 2
             43.3227  46.9346   8.8252 2
             33.3723   71.971 40.22082 2
             52.9648   58.018  20.5166 2
             20.2198  75.1588 13.31726 2
             22.4096  80.1646 22.08222 2
             36.8505  78.0906 24.54591 2
             18.5189  86.0365 13.32469 3
             29.3846  72.8176 21.77378 3
             37.4766  96.9463  23.6507 3
                32.9  82.0514 21.88465 1
             16.0277  28.5825  4.08484 1
             14.4336  83.8875 13.05017 1
             20.2658  57.8637  7.72334 1
             38.6757  67.8879 20.76761 1
             35.7355  81.2692 22.78104 1
              9.7347  37.9002 10.16035 1
             32.9494  63.6846 19.53084 1
             16.8288  49.5504  5.06637 1
              17.577  39.7841  5.07367 1
             22.6035  23.7174   7.7135 1
             24.6914  86.3752 13.54584 1
            46.92189 75.80044 23.03637 1
             23.0376  76.5371  23.8559 1
             22.7968   84.538 27.50557 1
            end
            label values income4 temp
            label def temp 1 "Low", modify
            label def temp 2 "Lower Middle", modify
            label def temp 3 "Upper Middle", modify
            label def temp 4 "High", modify
            
            statplot lly btot privo, over(income4) recast(bar)  asyvars showyvars leg(off)
            with graph
            Click image for larger version

Name:	llb00jme-f1a.png
Views:	1
Size:	66.2 KB
ID:	1550595



            My question is: how to deal with the unreadable labels on the X axis. (One way is below, but how to do that in Stata?)

            Click image for larger version

Name:	llb00jme-f1.png
Views:	1
Size:	34.7 KB
ID:	1550594



            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment


            • #7
              Two solutions:


              Code:
              statplot lly btot privo, over(income4) recast(bar)  asyvars
              
              statplot lly btot privo, over(income4) asyvars showyvars legend(off)

              Arguably this is a more mature graph yet:

              Code:
              collapse lly btot privo, by(income4)
              
              twoway connected lly btot privo income4, xla(, valuelabel) xtitle("")
              with extra work needed to add direct labelling | labeling, as discussed e.g. in https://www.stata-journal.com/articl...article=gr0080
              Last edited by Nick Cox; 02 May 2020, 01:54.

              Comment


              • #8
                Dear Nick, Thanks a lot. It really helps.
                Ho-Chuan (River) Huang
                Stata 19.0, MP(4)

                Comment


                • #9
                  Originally posted by River Huang View Post
                  Dear Nick, Following your example above, is it possible to have different colors for boy/girl? Thanks. [ATTACH=CONFIG]n1550351[/ATTACH]
                  Dear all,
                  Hope you are all fine.

                  Following the example, is there a way for me to include an additional bar in each category denoting the overall mean (both boys and girls) of each?
                  Any tip will be well received.

                  Best regards,
                  Ivan Manhique

                  Comment


                  • #10
                    Dear all,
                    Hope you are all fine.

                    Following the examples, is there a way for me to include an additional bar in each category denoting the overall mean for those graphs?
                    If any doubt what I want to produce is a graph like the attached to this reply.
                    Any tip will be well received.

                    Best regards,
                    Ivan Manhique
                    Click image for larger version

Name:	image001.png
Views:	1
Size:	53.9 KB
ID:	1556729

                    Comment

                    Working...
                    X