Announcement

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

  • Graph Bar: keep same bar color on different graph

    Dear Statalist, I'm working on several graph bar and this is my code:

    graph bar (sum) frequency
    if <several if condition, depending on report required>,
    over(Status)
    over(Type, label(labsize(vsmall)) )
    over(Year, label(labsize(vsmall)))
    bar(1, fcolor(cranberry) fintensity(inten100))
    bar(2, fcolor(dkorange) fintensity(inten100))
    bar(3, fcolor(dkgreen) fintensity(inten100))
    bar(4, fcolor(forest_green) fintensity(inten100))
    bar(5, fcolor(gold) fintensity(inten100))
    ytitle("")
    ylabel(, labsize(vsmall))
    title("This is my title", size(small))
    legend(on size(vsmall)
    symxsize(2) symysize(2))
    scheme(s2color)
    graphregion(fcolor(white) ifcolor(none))
    blabel(total, size(vsmall))
    bargap(20)

    As you can see, there is a dummy variable called frequency to count the number of record respecting to the group condition and there are 3 over() conditions. The first one is based on a variable called Status. This is a category with only five values. As espected, the bar option are used depending on the Status values presence in the dataset resulting from the <if> condition.

    e.g., if I have at least one recordset for each Status value, all the 5 bar option and colours are used, if I have a dataset with records containing only status 1, 4, 5, only the first 3 bar condition are used.

    Do you know if there is a way to bind the bar option to the Status value, in order to keep the same color for the same Status value? This will be very useful in reading similar graph, because the same Status same color.

    Thank you in advance.

    Giuseppe

  • #2
    This may help:

    Code:
    sysuse auto, clear
    bysort foreign rep78 : gen frequency = _N 
    separate frequency, by(rep78) veryshortlabel
    egen tag = tag(foreign rep78)  
    
    graph bar (asis) frequency? if tag & foreign, /// 
    over(rep78) nofill /// 
    bar(1, fcolor(cranberry) fintensity(inten100)) ///
    bar(2, fcolor(dkorange) fintensity(inten100))  ///
    bar(3, fcolor(dkgreen) fintensity(inten100))   ///
    bar(4, fcolor(forest_green) fintensity(inten100)) ///
    bar(5, fcolor(gold) fintensity(inten100)) name(g1, replace) 
    
    graph bar (asis) frequency? if tag & !foreign, /// 
    over(rep78) nofill /// 
    bar(1, fcolor(cranberry) fintensity(inten100)) ///
    bar(2, fcolor(dkorange) fintensity(inten100))  ///
    bar(3, fcolor(dkgreen) fintensity(inten100))   ///
    bar(4, fcolor(forest_green) fintensity(inten100)) ///
    bar(5, fcolor(gold) fintensity(inten100)) name(g2, replace)

    Comment


    • #3
      Giuseppe Briotti if I understand your question correctly, this would be exactly the type of scenario that brewscheme was intended to solve. You can create a scheme that defines a series of five colors for bar graphs only and then pass that scheme to the scheme option of the graph command to set the same aesthetics for each graph.

      Comment


      • #4
        wbuchanan, thanks, but probably due to my poor english I was not clear enough. I need to keep the same formatting style for bar, depending of the category value. So, if I have items in category 1,2,5 in the first graph, and 1,3,4,5 in the second one, I need that bars related with the category 1 and 5 have the same color in different graph. The two graph are identical, except for the if clause.
        Anyway, the brewscheme seems very interesting and I'll study it to improve graph automatic generation.

        Nick Cox you centered the point! Your example works fine. Now I need to study it a little to move it to my specific problem. Thanks!

        Comment


        • #5
          Hi Nick Cox
          I spent some time working on it. If I have understood well, because the (asis) option in Graph, the (over) and (if) combinations must identify a set of unique observations.
          This means that in by sort I must list all the variables involved in (over) and (if) clause, and of course the same list must be reported in the tag function. Is this correct or I'm missing something?

          Comment


          • #6
            My solution doesn't require the use of (asis): that's just an example.

            Comment


            • #7
              Thanks again Nick. I've found the problem with my dataset. It works fine!

              Comment

              Working...
              X