Announcement

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

  • Changing colour of bar graphs for one group

    Hi all,

    I'm using the following code to graph a figure by two sets of groupings - 'alt' and 'lcclass'. How do I edit the code to display the bars for class 2 in red? I want all 4 bars under class 1 in blue and the four under class 2 in red.

    Thank you.

    Code:
    graph hbar (mean) dth_hub_uptake, over(alt) over(lcclass) title(`"TH hub is available for mixed and TH visits (Basecase = not available)"', size(medsmall)) ytitle(`"Change in predicted uptake relative to base level"') ytitle(, size(small))


    Click image for larger version

Name:	hub1.tif
Views:	1
Size:	31.6 KB
ID:	1784939

  • #2
    I will use a different data example, as you don't give one.

    There are likely to be several ways to do what you want, but this one is quite general, and the trick has other applications.

    The principle is just this: Stata will happily use different colours if it is presented with different variables, so use separate first.

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . separate mpg, by(foreign) veryshortlabel
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------
    mpg0            byte    %8.0g                 Domestic
    mpg1            byte    %8.0g                 Foreign
    
    . graph hbar (mean) mpg? , over(rep78) over(foreign) nofill ysc(alt) legend(off) ytitle(Mean miles per gallon) bar(1, color(stc1)) bar(2, color(stc2))
    Click image for larger version

Name:	twobarcolours.png
Views:	1
Size:	28.8 KB
ID:	1784943



    Notes:

    1. See also

    Code:
    SJ-11-3 gr0049  . . . . . . . . . .  Stata tip 102: Highlighting specific bars
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            Q3/11   SJ 11(3):474--477
            tip on highlighting a subset of observations in a bar or
            dot chart
    https://journals.sagepub.com/doi/pdf...867X1101100310

    2. The undocumented option veryshortlabel can be helpful here and is explicit at (so in a sense documented within) https://journals.sagepub.com/doi/pdf...867X0500500412

    3. Putting the axis title at the top appeals personally whenever a graph has table flavour, but is utterly at choice. More at
    https://journals.sagepub.com/doi/pdf...867X1201200314

    4. Similarly the legend that would appear is in this case redundant and so I removed it.

    5. The bar colours I used are just the defaults in Stata 18 up with the default scheme stcolor. You would need to change them if you are not using Stata 18 up and you might want to change them for your application.

    6. There are no foreign cars with repair record 1 or 2 in the auto data. That is nothing to worry about.




    Comment


    • #3
      Thanks so much Nick, this worked.

      Comment

      Working...
      X