Announcement

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

  • Colouring individual bars of percent histogram whilst using by(var, cols(1)) command

    Hi there, I am plotting a percent histogram using a variable, impact, with 5 discrete categories, by another indicator variable, com. I am trying to colour each of the 5 bars individually: 1 red, 2 orange, 3 grey, 4 yellow, 5 green. I have tried plotting 5 different histograms and overlaying them, however, all the bars then read 100% and I am unable to use -cols(1)-. I would be grateful for some help!

    Here is some example data:

    impact com
    1 0
    3 0
    2 1
    1 0
    1 1
    4 0
    5 0
    2 0
    2 1
    2 1
    5 1
    4 1
    5 0
    5 1
    4 0
    3 1
    2 0
    2 1
    3 1
    3 0
    4 0
    1 0
    1 1
    5 0
    5 1
    3 0
    3 1
    4 0
    4 1
    2 1

    Here is my code:

    twoway histogram impact, percent by(com, cols(1)) discrete width(0.5) xlabel(1(1)5, valuelabel)

  • #2
    Red and green are not a good combination in graphics. Difficulty distinguishing them is a common challenge to sighted people.Otherwise this code may get you started in a better direction. There are naturally options like bar(1, ...) for bars 2 up.

    Code:
    clear
    input impact com
    1 0
    3 0
    2 1
    1 0
    1 1
    4 0
    5 0
    2 0
    2 1
    2 1
    5 1
    4 1
    5 0
    5 1
    4 0
    3 1
    2 0
    2 1
    3 1
    3 0
    4 0
    1 0
    1 1
    5 0
    5 1
    3 0
    3 1
    4 0
    4 1
    2 1
    end
    
    label def impact 1 some 2 suitable 3 series 4 of 5 adjectives
    label val impact impact
    
    graph bar (percent) , over(impact) by(com, note("")) legend(row(1)) asyvars bar(1, color(red))
    Otherwise see tabplot from the Stata Journal.

    EDIT Here are some examples using tabplot to give a flavour.


    Code:
    tabplot impact com, subtitle(% by com) percent(com) showval separate(impact) bar1(color(red)) name(G1)
    
    tabplot com impact, subtitle(% by com) percent(com) showval separate(impact) bar1(color(red)) name(G2)
    I like the principle that the outcome should go on the vertical axis -- unless the other way round gives a more effective graph.

    The so-called principle that you shouldn't use a graph and table is oversold. You can often get the best of both worlds with a hybrid display.

    To reverse the order of bars, use options yreverse or xreverse.

    To change the colours of bars 2 up, use options bar2() etc.

    Value labels can certainly be improved or supplied.

    Click image for larger version

Name:	impact_com1..png
Views:	1
Size:	16.3 KB
ID:	1724502


    Click image for larger version

Name:	impact_com1.png
Views:	1
Size:	16.9 KB
ID:	1724501
    Last edited by Nick Cox; 21 Aug 2023, 07:46.

    Comment

    Working...
    X