Announcement

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

  • Simple question re Bar Chart

    I am trying to create a simple bar chart to display percent Protein, Fat, and Carbohydrate in a small (n=8) series of small and large meals.

    graph bar PRO FAT CHO, over(MealType) creates the graph in the attached image.

    But what I am aiming for is the 2 bars for PRO to be next to one another, the the 2 bars for FAT, etc. I suppose I could create a graph for each one and then combine, but is there a more straightforward method?

    I think I know how to improve the axis labels, etc. But I can't figure out how to assemble the bars as I wish.

    Sorry if the answer is obvious, and appreciate any help!
    Tim

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(MealType PRO FAT CHO)
    0 25.6 28.5 45.9
    1  3.3 21.9 74.8
    0   14 30.4 55.5
    1 11.9 40.5 47.6
    0 19.6 33.6 46.8
    1 13.9 32.1   54
    0    9 25.9 64.1
    1 12.3 34.5 53.2
    end
    label values MealType TypeLabel
    label def TypeLabel 0 "Small", modify
    label def TypeLabel 1 "Large", modify
    Click image for larger version

Name:	BarChart.230717.JPG
Views:	2
Size:	30.5 KB
ID:	1720812

    Attached Files

  • #2
    Click image for larger version

Name:	meal.png
Views:	1
Size:	19.9 KB
ID:	1720908
    from SSC can help here. For example,

    Code:
    statplot ???, over(MealType) recast(bar) asyvars ytitle("percent")

    Comment


    • #3
      That should be

      statplot from SSC can help here.

      Adding variable labels would help. There is every reason to use e.g. Protein not PRO.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(MealType PRO FAT CHO)
      0 25.6 28.5 45.9
      1  3.3 21.9 74.8
      0   14 30.4 55.5
      1 11.9 40.5 47.6
      0 19.6 33.6 46.8
      1 13.9 32.1   54
      0    9 25.9 64.1
      1 12.3 34.5 53.2
      end
      label values MealType TypeLabel
      label def TypeLabel 0 "Small", modify
      label def TypeLabel 1 "Large", modify
      
      label var PRO "Protein"
      label var FAT "Fat"
      label var CHO "Carbohydrate"
      
      statplot ???, over(MealType) recast(bar) asyvars ytitle("percent")
      It seems that you are using scheme s2color in some version of Stata < 18. I am using Stata 18 and the default scheme stcolor.

      If you are using an old version of Stata it is a good idea to spell that out (FAQ Advice). If you are not using 18, not a big deal for this purpose, but my advice is to use just about any graph scheme but s2color.

      Comment


      • #4
        With graph bar, you need a long data layout. Unlike Nick, I do not have access to Stata 18, so I have to choose between the schemes s1mono and s1color.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(MealType PRO FAT CHO)
        0 25.6 28.5 45.9
        1  3.3 21.9 74.8
        0   14 30.4 55.5
        1 11.9 40.5 47.6
        0 19.6 33.6 46.8
        1 13.9 32.1   54
        0    9 25.9 64.1
        1 12.3 34.5 53.2
        end
        label values MealType TypeLabel
        label def TypeLabel 0 "Small", modify
        label def TypeLabel 1 "Large", modify
        
        rename (PRO FAT CHO) meal=
        gen obsno=_n
        reshape long meal, i(obsno) j(which) string 
        gr bar meal, over(MealType) over(which, relabel(1 "Carbohydrates" 2 "Fat" 3 "Protein")) ///
        ytitle("Percent") ylab("") ysc(r(0 60)) scheme(s1mono) asyvars ///
        leg(col(1) pos(1) region(lstyle(none)) ring(0)) blabel(total, format(%3.2f))
        gr_edit .plotregion1.style.editstyle boxstyle(linestyle(color(none))) editcopy
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	19.1 KB
ID:	1720934

        Comment


        • #5
          Comparing #2/#3 and #4

          1. statplot doesn't reshape but its main strategy is equivalent, to produce a different data layout on the fly.

          2. statplot allows blabel(bar) in the usual way. I think that's a good idea but wouldn't defend 2 d.p. personally!

          3. Just to underline that statplot is for version 8.2 up.

          4. Although statplot does (I suggest) what it was intended to do, I agree with what I take to be Andrew Musau's main point, that you need a different layout and that's not especially difficult (but not "obvious" as is modestly feared in #1).

          Here's yet another take, just using frames, the point of which is that your original data layout remains.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(MealType PRO FAT CHO)
          0 25.6 28.5 45.9
          1  3.3 21.9 74.8
          0   14 30.4 55.5
          1 11.9 40.5 47.6
          0 19.6 33.6 46.8
          1 13.9 32.1   54
          0    9 25.9 64.1
          1 12.3 34.5 53.2
          end
          label values MealType TypeLabel
          label def TypeLabel 0 "Small", modify
          label def TypeLabel 1 "Large", modify
          
          capture frame drop sandbox 
          
          frame put *, into(sandbox)
          
          frame sandbox { 
              gen id = _n 
              rename (PRO FAT CHO) (value#), addnumber 
              reshape long value, i(id) j(which)
              label def which 1 Protein 2 Fat 3 Carbohydrate 
              label val which which 
              
              graph bar (mean) value, over(MealType) ytitle(percent) yla(none)  over(which) asyvars blabel(bar, format(%2.1f))
          }
          Click image for larger version

Name:	meal2.png
Views:	1
Size:	19.1 KB
ID:	1720954

          Comment


          • #6
            Thank you both very much!!
            You solved the problem and gave me a bunch of new things to learn.
            Much appreciated!
            Tim

            Comment


            • #7
              Thanks again!
              I've now (more or less) understood the reshaping method, so I'm good with that.
              I may be getting in over my head, but is it possible to add error bars? Not essential, but would be nice.
              Tim

              Comment


              • #8
                graph bar and graph dot aren't compatible with showing intervals as capped spikes or uncapped spikes. It's best in my view to change to twoway. Here's one approach.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float(MealType PRO FAT CHO)
                0 25.6 28.5 45.9
                1  3.3 21.9 74.8
                0   14 30.4 55.5
                1 11.9 40.5 47.6
                0 19.6 33.6 46.8
                1 13.9 32.1   54
                0    9 25.9 64.1
                1 12.3 34.5 53.2
                end
                label values MealType TypeLabel
                label def TypeLabel 0 "Small", modify
                label def TypeLabel 1 "Large", modify
                
                capture frame drop sandbox 
                
                frame put *, into(sandbox)
                
                frame sandbox { 
                    gen id = _n 
                    rename (PRO FAT CHO) (value#), addnumber 
                    reshape long value, i(id) j(which)
                    label def which 1 Protein 2 Fat 3 Carbohydrate 
                    label val which which 
                    
                    statsby, by(MealType which) clear: ci means value 
                    
                    scatter mean MealType, ytitle(percent) xsc(r(-0.3 1.3)) xla(0 1, valuelabels) by(which, note("") row(1) legend(off)) ///
                    || rcap ub lb MealType, msize(large)
                }

                Click image for larger version

Name:	meal3.png
Views:	1
Size:	26.2 KB
ID:	1721182

                Comment


                • #9
                  Interesting. I have used twoway in the past--following some other guidance from you I found on StataList.
                  Thanks again!!
                  Tim

                  Comment

                  Working...
                  X