Announcement

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

  • Color bars in graph based on third variable and display bar label

    I am attempting to display the change in insurance coverage "CHANGE" for each state (n=51 incl. DC). For each state I also have a variable that indicates the state's policy status (none, full, early). I would like a bar graph that shows a bar for each state (preferably sorted by magnitude of change) with the state abbreviation above the bar and the bar colored to indicate its expansion status.

    The code below colors the bars differently (although I am afraid it is not coloring the bars correctly for each state since I sort the bars) but does not result in the state's abbreviation being shown above the relevant bar. If I remove the code beginning at 'asyvar', the abbreviation is displayed above the bar. If I keep that code but change to blabel(bar), the value is displayed above the bar.

    graph bar CHANGE, over(STATE, gap(30) sort(1)) blabel(group, position(outside) size(tiny)) legend(off) ///
    asyvars ///
    bar(1, color(orange)) ///
    bar(2, color(green)) ///
    bar(3, color(blue)) ///
    bar(4, color(blue)) ///
    bar(5, color(yellow)) ///
    ...
    bar(51, color(orange))

    Is it possible to create a single bar graph? I have contemplated creating separate graphs for each policy status. Thanks!

  • #2
    I can't see your data or your graph or even easily imagine your graph. I imagine you are referring to U.S. states. (Please remember that this is an international forum, so allusion to 51 and DC might not be self-explanatory to all.)

    Let me start from somewhere else and try to travel towards your question.

    The essential ingredients appear to be

    1. 50 (or so) observations with meaningful names you want to show.

    2. A measured variable to show by bar lengths or heights.

    3. A grouping variable with 3 (or so) categories (how orange green blue yellow map onto those presumably is explicable).

    With apologies to DC (I enjoying visiting during the Stata Conference in 2009), here is something similar.

    Code:
    sysuse census, clear 
    separate medage, by(region) veryshortlabel
    graph hbar (asis) medage? , over(state, sort(medage) label(labsize(*.7))) ysize(7) nofill legend(pos(3) col(1))
    Here it is

    Click image for larger version

Name:	barchart.png
Views:	1
Size:	17.4 KB
ID:	1321993


    Clearly you might improve matters in various ways, e.g. using state abbreviations not names as you have stated, or fiddling with the position of the legend (I don't see how switching it off would make your categories clear). Readability seems to remain a problem.

    It's a strong prejudice of mine that horizontal bar charts are more legible.

    Does this help?

    Comment


    • #3
      Here's another go. (This makes explicit what was true earlier, that the graph scheme is s1color.)

      Code:
      sysuse census, clear
      set scheme s1color
      separate medage, by(region) veryshortlabel
      sort medage
      gen hilo = _n > 25
      label def hilo 0 "lowest 25" 1 "highest 25"
      label val hilo hilo
      graph hbar (asis) medage? , by(hilo, title(Median ages by U.S. state) note("")) over(state, sort(medage)) ///
      ysize(7) nofill legend(pos(3) col(2)) subtitle(, bcolor(bg))
      Click image for larger version

Name:	barchart2.png
Views:	1
Size:	43.1 KB
ID:	1321998

      Comment


      • #4
        Good afternoon Nick,
        Your suggested code worked perfectly. I have a little fiddling to with titles, labels, etc., but the display is exactly what I wanted. Thanks.

        Comment


        • #5
          I played around some more. If you move to dot charts, the scale doesn't have to start at zero. There is some extra trickery to ensure that Stata draws every horizontal line.

          Code:
          graph dot (asis) medage? medage , by(hilo, title(Median ages by U.S. state) note("")) over(state, sort(medage)) ///
          ysize(7) nofill legend(order(1 2 3 4) pos(3) col(2)) subtitle(, bcolor(bg)) ///
          linetype(line) lines(lc(gs12) lw(vthin)) yla(20(5)35, grid) ytic(21/34) exclude0 ///
          marker(1, ms(Oh)) marker(2, ms(+)) marker(3, ms(Th)) marker(4, ms(D)) marker(5, ms(none))
          Click image for larger version

Name:	barchart3.png
Views:	1
Size:	50.4 KB
ID:	1322097

          Comment


          • #6
            Thanks for the additional graph suggestions. I'd like to change the colors of the bars to be shades of basically two colors -- depending on the category (region in your example). I attempted to use 'bar' but where I included it in the code -- within the 'over()' option -- Stata politely told me it is not allowed. Anyway I can change colors?

            Comment


            • #7
              Please show your command and its exact results to get precise advice. You could follow my posts because I posted code and graphs to a dataset you could use yourself: that works both ways!

              Comment


              • #8
                Will be sure to include commands and results going forward. I do appreciate the help!

                This is the code I tried with the intention of coloring P3, P4, and P5 bars with the same color but differing intensities; P6 and P7 the same color but differing intensities.

                graph bar (asis) Change2014?, over(STATE, axis(off) sort(Change2014, bar((1, color(red)) bar(2, color(red*.5)) bar(3, color(blue*.25)) ///
                bar(4, color(blue*.5)) bar(5, color(blue)) bar(6, color(green)) bar(7, color(green*.5)))) label(labsize(*.6))) ///
                blabel(group, position(outside) size(vsmall)) ///
                ysize(3) nofill legend(pos(6) row(2) ///
                label(1 "P0") label(2 "P1") label(3 "P2") label(4 "P3") label(5 "P4") label(6 "P5") label(7 "P6") size(vsmall)) ///
                title("Change from 2013 to 2014", size(medium)) ///
                ytitle("M_Change")


                The resulting graph does not change the color of the bars from the s1scheme from your original code. I have attached the resulting graph. Everything except the color is perfect for what I wish to display.



                Comment


                • #9
                  You have the bar colour options as suboptions of sort(). That looks wrong to me.

                  Comment


                  • #10
                    I also tried the following code with the bar color options as a suboption of over() and get the error message "option bar() not allowed"

                    graph bar (asis) Change2014?, over(STATE, axis(off) bar(1, color(red)) bar(2, color(red*.5)) bar(3, color(blue*.25)) ///
                    bar(4, color(blue*.5)) bar(5, color(blue)) bar(6, color(green)) bar(7, color(green*.5)) sort(Change2014) label(labsize(*.6))) ///
                    blabel(group, position(outside) size(vsmall)) ///
                    ysize(3) nofill asyvars legend(pos(6) row(2) label(1 "P0") label(2 "P1") label(3 "P2") label(4 "P3") label(5 "P4") label(6 "P5") label(7 "P6") size(vsmall)) ///
                    title("Change from 2013 to 2014", size(medium)) ///
                    ytitle("M_Change")

                    Comment


                    • #11
                      One more attempt... The following code resulted in the bars changing color but now my sorting is off...

                      graph bar (asis) Change2014?, over(STATE, axis(off) sort(Change2014?) label(labsize(*.6))) bar(1, color(red)) bar(2, color(red*.5)) bar(3, color(blue*.25)) ///
                      bar(4, color(blue*.5)) bar(5, color(blue)) bar(6, color(green)) bar(7, color(green*.5)) ///
                      blabel(group, position(outside) size(vsmall)) ///
                      ysize(3) nofill asyvars legend(pos(6) row(2) label(1 "P0") label(2 "P1") label(3 "P2") label(4 "P3") label(5 "P4") label(6 "P5") ///
                      label(7 "P6") size(vsmall)) ///
                      title("Change from 2013 to 2014", size(medium)) ///
                      ytitle("M_Change")


                      Comment


                      • #12
                        At a guess sort on your original variable, not the separated variables.

                        Code:
                        sort(Change2014)
                        Please follow advice on not using photo attachments. FAQ Advice 12.

                        Comment


                        • #13
                          If I remove asyvars from the code, it works -- sorting and coloring as I wanted. Thanks for all of your help!

                          Comment


                          • #14
                            Thanks for the closure.

                            People interested in this thread may enjoy http://www.statalist.org/forums/foru...x-for-graphics

                            The distinction there between values above and below 50% is graphically the same question as distinguishing values above and below zero.

                            Comment

                            Working...
                            X