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

  • How to color bars by gender


    I want to plot some estimated betas from an foregoing regression. I have 140 betas of 140 individuals. I want to order the betas by size and plot them. So far I use

    graph bar (asis) betas, over(newid, sort(betas))

    were betas is a variable containing the estimated beta for each inidividuum and newid is a unique identifiert going from 1-140.

    I attached the graph, one bar stands for one estimated beta of one person.

    Now I would like to color the bars depending on the gender of the individuals e.g blue for women and red for men. I could use the variable femaleD which is equal to one if an individual is a woman and zero otherwise.

    Does anyone has a suggestion how to do this?

    Thanks a lot in advance.

    Felix Schmidt

    Attached Files

  • #2
    You can do this with graph bar, but it's easier with twoway bar, particularly as you will want to get rid of the noise that the category labels have become.

    This should help. The graph needs further refinement.

    sysuse auto, clear
    sort mpg
    su mpg, meanonly 
    gen show = mpg - r(mean) 
    gen order = _n
    twoway bar show order if foreign, bcolor(red*0.5) || bar mpg order if !foreign, bcolor(blue*0.5)


    • #3
      Welcome to the Stata Forum / Statalist.

      I gather the option - asyvars - may do the trick for you. Since you didn't share data, it cannot be tested.

      P.S.: crossed with Nick's clarifying reply. That said, taking the example in #2, maybe the command -graph bar (asis) show, over(foreign) over(order) asyvars - can do the trick for you as well.
      Last edited by Marcos Almeida; 14 Feb 2018, 08:16.
      Best regards,



      • #4
        Thanks a lot Nick!

        I used:

        sort betas
        gen order = _n
        twoway bar betas order if femaleD, bcolor(red*0.5) || bar betas order if !femaleD, bcolor(blue*0.5)

        and it works just fine!

        Attached Files


        • #5
          Good; I would still recommend quantile-box plots here!