Announcement

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

  • changing the color of one bar

    I want to change the color of one bar in a bar chart, but the bar() option does not seem to help.
    Stata/SE 17.0 on Win10
    Here is a minimal working example:

    cls
    clear all
    input state date freq
    3 1 14
    3 2 15
    3 3 11
    3 4 15
    2 1 4
    2 2 4
    2 3 1
    2 4 4
    1 1 1
    1 2 7
    1 3 4
    1 4 5
    end
    graph bar freq, over(date) stack bar(2, color(red)) by(state)

  • #2
    It does work.... In this sense: the syntax is legal and Stata would colour your second outcome variable red if you had one, but you don't.

    What you want might be:

    1. The second bar (really just one bar) should be red.

    2. The second category of date should be red.

    3. The second category of
    state should be red.

    You don't say which very clearly, but this might help:


    Code:
    separate freq,  by(state == 2)
    graph bar (asis) freq0 freq1, over(date) stack bar(2, color(red)) by(state, legend(off))
    If it is really just one bar, the command might be

    Code:
    separate freq, by(state == 1 & date == 2)
    See also https://www.stata-journal.com/articl...article=gr0049
    Last edited by Nick Cox; 16 Oct 2021, 05:42.

    Comment


    • #3
      Perfect solution, thank you!
      And sorry for being vague. I meant "The second category of date should be red." which you clearly answered.
      Last edited by Maysam Rabbani; 17 Oct 2021, 02:11.

      Comment


      • #4
        Hi Nick Cox , I attempted doing this to create a horizontal bar chart, but there seems to be a weird gap between the bar I chose to separate and the others. Would you know how to resolve this or why it is happening?


        Click image for larger version

Name:	test_2.png
Views:	1
Size:	50.1 KB
ID:	1754394
        Code:
        separate value, by(state=="USA")
        
        
        graph hbar (asis) value0 value1, over(state, sort(value) label(labsize(medium)) gap(10)) ///
        blabel(bar, color(black) size(medium) format(%9.1f)) ///
        ylabel(,labsize(medium)) ///
        bar(2, fcolor(red)) ///
        legend(off) ///
        scale(*.5) ///
        xsize(33) ///
        ysize(23) ///
        b1title("% reported XX", color(black) size(medium))
        Last edited by Vishal Sai; 24 May 2024, 05:47.

        Comment


        • #5
          Although in this case I could guess the problem immediately, a data example always helps.

          Your data is similar to this and you need to specify the nofill option.

          The frequent need for nofill was a detail flagged in the paper linked in #2.

          Pedantically or otherwise: a spelling "New york" is likely to be considered an error by anyone grading or reviewing your work. You should say "New York".

          Code:
          clear
          input str8 state value
          "USA"          10
          "Texas"       12
          "Alabama"     14
          "New York"    16
          end
          
          separate value, by(state=="USA")
          
          
          graph hbar (asis) value0 value1, nofill over(state, sort(value) label(labsize(medium)) gap(10)) ///
          blabel(bar, color(black) size(medium) format(%9.1f)) ///
          ylabel(,labsize(medium)) ///
          bar(2, fcolor(red)) ///
          legend(off) ///
          scale(*.5) ///
          b1title("% reported XX", color(black) size(medium))
          Last edited by Nick Cox; 24 May 2024, 07:25.

          Comment


          • #6
            Your solution fixed the problem, thanks a lot. We were given few of your papers as readings during my stats course at university. I’ll give this one a read too! And thank you for the feedback on the lowercase "New York" :')

            Comment


            • #7
              Thanks for the closure and comments.

              Comment

              Working...
              X