Announcement

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

  • How to tweak a bar graph

    Hello,

    I am using the following commands to produce the graph below:

    #delimit ;
    graph bar, over(COUNTRY) over (PEMP1921cat)
    asyvars
    blabel(bar, format(%9.1f))
    percentages
    bar(1, color(blue))
    bar(2, color(orange))
    ytitle("Number of companies");

    For each of the three categories of PEMP1921cat, the bars show the percentage of companies in each country (CAN or USA) relative to the total number of companies in both countries, for a total of 100%.

    I would prefer having either the number of companies for each country by category or having the percentage of companies in each category relative to the total number of companies in total for that same category. In other words, if there were 100 Canadian companies in total that were distributed 20, 40, 40 in the three categories, I would like the bars to show either the counts (20, 40, 40) or, even better, the corresponding percentages, i.e. 20%, 40% and 40%. Same for the US companies.

    I tried different commands without success. Is this feasible?

    Thank you!

    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	25.0 KB
ID:	1678991

  • #2
    Data example please.

    Comment


    • #3
      And you aren't happy with the results of just switching the order of your -over- options?

      Compare
      Code:
      sysuse auto, clear
      #delimit ;
      graph bar, over(rep78) over(foreign) 
          asyvars 
          blabel(bar, format(%9.1f)) 
          percentages
          scheme(s2color)
          ;
      #delimit cr
      which produces:
      Click image for larger version

Name:	Screenshot 2022-08-24 at 7.54.12 AM.png
Views:	2
Size:	161.0 KB
ID:	1679000
      with the results from
      Code:
      sysuse auto, clear
      
      #delimit ;
      graph bar,  over(foreign) over(rep78) 
          asyvars 
          blabel(bar, format(%9.1f)) 
          percentages
          scheme(s2color)
          ;
      #delimit cr
      which produces:
      Click image for larger version

Name:	Screenshot 2022-08-24 at 7.55.12 AM.png
Views:	2
Size:	189.4 KB
ID:	1679002
      Attached Files

      Comment


      • #4
        No, switching the options does not produce what I want. Please find the data attached. Thanks!
        Attached Files

        Comment


        • #5
          No no no, I mean a data example using dataex

          Comment


          • #6
            Originally posted by Claude Francoeur View Post
            No, switching the options does not produce what I want. Please find the data attached. Thanks!
            Well, then I'm not quite sure what you want. Switching the options does produce a graph with three bars each for CAN and USA, with their respective distributions over the three categories of PEMP1921cat. If that's not what you want, you'll need to explain what you want different.

            Comment


            • #7
              Jared Greathouse is alluding to the longstanding request at https://www.statalist.org/forums/help#stata to post examples using dataex.
              You're asked to read the FAQ Advice before posting.


              For graphical purposes, Claude's dataset with 3713 observations is equivalent to this result from contract

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input str3 LOC float PEMP1921cat int _freq
              "CAN" 0  181
              "CAN" 1  140
              "CAN" 2  153
              "USA" 0 1056
              "USA" 1 1098
              "USA" 2 1085
              end
              label values PEMP1921cat PEMP1921cat
              label def PEMP1921cat 0 "-.6473907-", modify
              label def PEMP1921cat 1 "-.0597015-", modify
              label def PEMP1921cat 2 ".1069561-", modify


              Then here is some code. I used catplot from SSC.


              Code:
              catplot LOC PEMP1921cat [fw=_freq] , recast(bar) percent(LOC) asyvars bar(1, lcolor(blue) fcolor(blue*0.5)) bar(2, lcolor(red) fcolor(red*0.5)) blabel(total, format(%2.1f)) t1title(explain the categories somewhere)


              Click image for larger version

Name:	catplot3.png
Views:	1
Size:	25.2 KB
ID:	1679134


              Comment


              • #8
                That works fine. Thank you Nick! And I will use dataex next time.

                Comment

                Working...
                X