Announcement

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

  • Bar graph conditional formatting

    Almost totally new at this...

    I would like to create a bar graph and apply conditional formatting to select cells in order to change their color.

    In the data table below I used:

    graph hbar (asis) conditiona conditionb, over(goterm, sort(order))

    Now I would like to use the variables pofa, pofb to selectively color bars that fall below certain thresholds.

    So for example pathway one, condition b, pofb<0.01 so I would like to set the color of that bar to red, but pathwaytwo, conditionb, pofb>0.01 so that bar would be set to black.

    This problem may be trivially easy, but I could not solve it with the available documentation that I found in the manual.

    Thanks in advance for any tips!
    goterm conditiona conditionb pofa pofb
    pathwayone 1.2 -2.5 0.1 0.01
    pathwaytwo -1.25 -2.49 0.01 0.1
    pathwaythree 2.19 -2.42 0.01 0.01

  • #2
    reshape your data such that you can specify an identifier for your condition.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str15 goterm float(conditiona conditionb pofa pofb)
    "pathwayone"     1.2  -2.5  .1 .01
    "pathwaytwo"   -1.25 -2.49 .01  .1
    "pathwaythree"  2.19 -2.42 .01 .01
    end
    
    reshape long condition pof, i(goterm) j(which) string
    replace which= cond(which=="a", "condition a", "condition b")
    gen group= pof>0.01
    seperate condition, by(group)
    graph hbar condition?,   over(which) over(goterm) bar(1, color(red)) ///
    bar(2, color(black)) leg(off) note("Note: Black bars represent pof>0.01") ///
    scheme(s1color)

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	38.0 KB
ID:	1565639

    Comment


    • #3
      Andrew, this is super helpful! One thing I don't understand is the function of this line in your code:

      replace which= cond(which=="a", "condition a", "condition b")
      And whether this line is necessary?

      Comment


      • #4
        This just changes the label from "a" and "b" to "condition a" and "condition b", respectively. Not necessary for the functioning of the code.

        Comment


        • #5
          Andrew, Thanks so much for lending your expertise. I got your code to work perfectly and it looks great.

          Is it possible to preserve the color by which (e.g. blue for one category, green for the other) and apply shading based on the group?

          Comment


          • #6
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str15 goterm float(conditiona conditionb pofa pofb)
            "pathwayone"     1.2  -2.5  .1 .01
            "pathwaytwo"   -1.25 -2.49 .01  .1
            "pathwaythree"  2.19 -2.42 .01 .01
            end
            
            reshape long condition pof, i(goterm) j(which) string
            replace which= cond(which=="a", "condition a", "condition b")
            gen group= pof>0.01
            egen bygroup= group(group which)
            separate condition, by(bygroup)
            graph hbar condition?, over(which) over(goterm) bar(1, color(blue)) ///
            bar(2, color(green)) bar(3, color(blue%40)) bar(4, color(green%40)) nofill ///
            leg(off) note("Note: Light-shaded bars represent pof>0.01") scheme(s1color)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	38.3 KB
ID:	1565836

            Last edited by Andrew Musau; 28 Jul 2020, 23:43.

            Comment


            • #7
              so helpful! is it possible to achieve the same ordering as you get here using 'over(which)' but suppress that? I tried setting 'over(which, lab(labsize(zero)))' which is close to what I am looking for, but i want it to be essentially as if there was no over(which) command. the issue is that stata will order the bars based on the magnitude of condition? which is not desirable.

              Comment


              • #8
                Then just sort using which, and you will then need a legend. Also note that if you want to adjust intensity without affecting opacity, use color*# instead of color%# as below.

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input str15 goterm float(conditiona conditionb pofa pofb)
                "pathwayone"     1.2  -2.5  .1 .01
                "pathwaytwo"   -1.25 -2.49 .01  .1
                "pathwaythree"  2.19 -2.42 .01 .01
                end
                
                reshape long condition pof, i(goterm) j(which) string
                replace which= cond(which=="a", "condition a", "condition b")
                gen group= pof>0.01
                egen bygroup= group(group which)
                separate condition, by(bygroup)
                graph hbar condition?, over(goterm, sort(which)) bar(1, color(blue)) ///
                bar(2, color(green)) bar(3, color(blue*0.5)) bar(4, color(green*0.5)) nofill ///
                leg(order(1 "condition a" 2 "condition b") col(1) ) note("Note: Light-shaded ///
                bars represent pof>0.01") scheme(s1color)

                Comment


                • #9
                  Andrew, in this example the order of the bars changes by (goterm): so it goes green, blue then blue, green, etc (see .png below)

                  I don't understand what information Stata is using to pick which bar to list first??

                  I'd like to have a consistent order, so it always puts blue bar first, for example. In my real dataset I tried creating a new variable and sorted on that, but it still swaps the order for some reason I can't figure out.

                  Click image for larger version

Name:	example.png
Views:	1
Size:	116.6 KB
ID:	1565956


                  Comment


                  • #10
                    You are right, sorry - I did not pay attention to the rendered graph. It appears that Stata will not sort within over categories, so our best bet is to replicate the wanted graph using the two over variables. Unless I am missing something, the main difference lies in the widths of the bars and spacing between grouped bars, which we can control. Here is another try:

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input str15 goterm float(conditiona conditionb pofa pofb)
                    "pathwayone"     1.2  -2.5  .1 .01
                    "pathwaytwo"   -1.25 -2.49 .01  .1
                    "pathwaythree"  2.19 -2.42 .01 .01
                    end
                    
                    reshape long condition pof, i(goterm) j(which) string
                    replace which= cond(which=="a", "condition a", "condition b")
                    gen group= pof>0.01
                    egen bygroup= group(group which)
                    separate condition, by(bygroup)
                    gen order=_n
                    graph hbar condition?, over(which, lab(nolab) gap(*0)) over(goterm, gap(*0.2)) ///
                    bar(1, color(blue)) bar(2, color(green)) bar(3, color(blue*0.5)) ///
                    bar(4, color(green*0.5)) nofill leg(order(1 "condition a" 2 "condition b") ///
                    col(1) ) note("Note: Light-shaded bars represent pof>0.01") scheme(s1color)
                    Click image for larger version

Name:	Graph.png
Views:	1
Size:	53.9 KB
ID:	1565960

                    Comment


                    • #11
                      I can not thank you enough for your help with this! The expertise and generosity of this group is truly stunning.

                      Comment


                      • #12
                        Hi,

                        Is it possible to adjust the width of the bars in the graph while keeping the overall dimensions of the graph the same? I can't find a command that will do this for a graph hbar.

                        For example: I'd like to reduce the width of the bars in the graph above by 50%, is that possible?

                        Thanks,

                        Yariv

                        Comment


                        • #13
                          Don't you just increase the gap between bars?

                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input str15 goterm float(conditiona conditionb pofa pofb)
                          "pathwayone"     1.2  -2.5  .1 .01
                          "pathwaytwo"   -1.25 -2.49 .01  .1
                          "pathwaythree"  2.19 -2.42 .01 .01
                          end
                          
                          reshape long condition pof, i(goterm) j(which) string
                          replace which= cond(which=="a", "condition a", "condition b")
                          gen group= pof>0.01
                          egen bygroup= group(group which)
                          separate condition, by(bygroup)
                          gen order=_n
                          graph hbar condition?, over(which, lab(nolab) gap(*0)) over(goterm, gap(*0.2)) ///
                          bar(1, color(blue)) bar(2, color(green)) bar(3, color(blue*0.5)) ///
                          bar(4, color(green*0.5)) nofill leg(order(1 "condition a" 2 "condition b") ///
                          col(1) ) note("Note: Light-shaded bars represent pof>0.01") scheme(s1color)
                          gr save gr1, replace
                          graph hbar condition?, over(which, lab(nolab) gap(*0)) over(goterm, gap(*0.7)) ///
                          bar(1, color(blue)) bar(2, color(green)) bar(3, color(blue*0.5)) ///
                          bar(4, color(green*0.5)) nofill leg(order(1 "condition a" 2 "condition b") ///
                          col(1) ) note("Note: Light-shaded bars represent pof>0.01") scheme(s1color)
                          gr save gr2, replace
                          gr combine gr1.gph gr2.gph, scheme(s1color)
                          Click image for larger version

Name:	Graph.png
Views:	1
Size:	65.7 KB
ID:	1569614

                          Comment

                          Working...
                          X