Announcement

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

  • Different colored bars and chart legend

    I created bar charts for a variable (p1m10).
    I wanted to see how the the value differs across another variable (grp).
    I further wanted to see how the distribution of the variable across groups differs across genders.
    And then I wanted to see these charts next to each other.
    So I used the following code:
    Code:
    graph bar p1m10 , over(grp,label(labsize(vsmall) ))  over(female)  ytitle(Percent supporting project 1) name(percentproject1bygender)
    graph bar p2m10 , over(grp,label(labsize(vsmall) ))  over(female)  ytitle(Percent supporting project 2) name(percentproject2bygender)
    graph bar p3m10 , over(grp,label(labsize(vsmall) ))  over(female)  ytitle(Percent supporting project 3) name(percentproject3bygender)
    graph combine percentproject1bygender percentproject2bygender percentproject3bygender, col(3)
    This generated the exact chart that I want:
    Click image for larger version

Name:	percentsupportprojectgenderwithoutdonation.png
Views:	1
Size:	47.7 KB
ID:	1663846

    In each of the 3 charts above, you can see 2 distributions: first for males (and other genders) and second for females.
    Within each distribution, there are 3 groups, represented by a bar each.
    As you can see, the group names are obscured by the small font size.

    I would like to give each group a specific color, e.g. for the 3 groups, I could choose black, blue, green.

    And then have a legend saying
    black: group name 1
    blue: group name 2
    green: group name 3

    However:
    1. I am not sure how to change colors of individual bars
    2. Since I have 3 charts combined, I am not sure how to have a single legend across them...


    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

  • #2
    I think the answer to your specific question is the option asyvars.

    But these graphs seem to me seriously problematic

    1. much of the real estate of the graph is bar shading showing that the means concerned are all not zero; you'd be better off with graph dot or some equivalentallowing focus on the interesting variation from about 0.4 to about 0.8

    2. the smaller horizontal axis labels are illegible (they are already vsmall yet show substantial overlap)

    3. the vertical axis title confuses percent (0 - 100) and proportion (0 - 1)

    4. as you say, the graphs should be combined more subtly and less repetitively


    The graph shows 18 means. If you showed the results of

    Code:
      
     collapse p1m10  p2m10 p3m10, by(grp female)   dataex


    that would give a sandbox for people to show other possibilities.




    Comment


    • #3
      I roughed some data by eye.


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(p1m10 p2m10 p3m10 female grp) 
       .8  .6 .78 0 1 
      .68 .48 .59 0 2 
      .82 .58 .72 0 3 
       .8 .64 .74 1 1 
      .62 .56 .73 1 2 
      .92 .66 .88 1 3 
      end
      
      reshape long p@m10, i(female grp) j(project) 
      
      label def female 0 "Male etc." 1 "Female"
      label val female female 
      
      sort project female grp 
      rename pm10 mean 
      
      list 
      
      replace mean = 100 * mean
      set scheme s1color  
      graph dot (asis) mean , over(grp) over(female) yla(40(10)90) by(project, subtitle("     Percent supporting each project") /// 
       col(1) note("") legend(pos(3) col(1))) exclude0 asyvars ysc(alt) linetype(line) lines(lc(gs12) lw(vthin)) legend(col(1)) ///
      marker(1, ms(Oh) mc(black)) marker(2, ms(+) mc(blue)) marker(3, ms(X) mc(green)) subtitle(, size(large) color(red) pos(9) nobox nobexpand fcolor(none))
      Click image for larger version

Name:	project.png
Views:	1
Size:	21.3 KB
ID:	1663956

      Last edited by Nick Cox; 10 May 2022, 15:09.

      Comment


      • #4
        That is awesome, many thanks Nick Cox
        Thank you for your help!

        Stata SE/17.0, Windows 10 Enterprise

        Comment


        • #5
          You should have some scope for showing not too long value labels for grp in the legend.

          Comment

          Working...
          X