Announcement

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

  • hbar conditional coloring and formatting

    I need some help formatting and selectively coloring some bars in the below graph. All US Gulf states (AL, MS, LA and TX) are colored blue, "US" is colored red, and the rest are colored black. As you can see from the below image, the spacing between the bars is not uniform. Can you please suggest any tips that can help make the graph look better with thicker and uniformly spaced bars. Any help in also removing the top/bottom white space and aligning labels with bars is appreciated.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year double price str181 state str3 region double(price0 price1 price00 price01)
    2022 18.43 "AK" ""    18.43    . 18.43    .
    2022  7.72 "AL" "GOM"  7.72    .     . 7.72
    2022  7.38 "AR" ""     7.38    .  7.38    .
    2022  7.86 "AZ" ""     7.86    .  7.86    .
    2022 17.09 "CA" ""    17.09    . 17.09    .
    2022  8.63 "CO" ""     8.63    .  8.63    .
    2022 15.07 "CT" ""    15.07    . 15.07    .
    2022  7.74 "DC" ""     7.74    .  7.74    .
    2022  8.79 "DE" ""     8.79    .  8.79    .
    2022  9.16 "FL" ""     9.16    .  9.16    .
    2022  8.65 "GA" ""     8.65    .  8.65    .
    2022  7.06 "IA" ""     7.06    .  7.06    .
    2022  6.71 "ID" ""     6.71    .  6.71    .
    2022  8.57 "IL" ""     8.57    .  8.57    .
    2022  8.65 "IN" ""     8.65    .  8.65    .
    2022   8.3 "KS" ""      8.3    .   8.3    .
    2022  7.41 "KY" ""     7.41    .  7.41    .
    2022  7.54 "LA" "GOM"  7.54    .     . 7.54
    2022 17.06 "MA" ""    17.06    . 17.06    .
    2022 10.01 "MD" ""    10.01    . 10.01    .
    2022 11.03 "ME" ""    11.03    . 11.03    .
    2022  8.33 "MI" ""     8.33    .  8.33    .
    2022  9.25 "MN" ""     9.25    .  9.25    .
    2022  7.67 "MO" ""     7.67    .  7.67    .
    2022  6.71 "MS" "GOM"  6.71    .     . 6.71
    2022  7.49 "MT" ""     7.49    .  7.49    .
    2022  6.54 "NC" ""     6.54    .  6.54    .
    2022  7.28 "ND" ""     7.28    .  7.28    .
    2022  7.21 "NE" ""     7.21    .  7.21    .
    2022 15.15 "NH" ""    15.15    . 15.15    .
    2022 12.12 "NJ" ""    12.12    . 12.12    .
    2022  6.56 "NM" ""     6.56    .  6.56    .
    2022   8.5 "NV" ""      8.5    .   8.5    .
    2022  7.55 "NY" ""     7.55    .  7.55    .
    2022  7.45 "OH" ""     7.45    .  7.45    .
    2022  6.96 "OK" ""     6.96    .  6.96    .
    2022  6.81 "OR" ""     6.81    .  6.81    .
    2022  8.21 "PA" ""     8.21    .  8.21    .
    2022 17.96 "RI" ""    17.96    . 17.96    .
    2022  7.13 "SC" ""     7.13    .  7.13    .
    2022  8.04 "SD" ""     8.04    .  8.04    .
    2022  6.55 "TN" ""     6.55    .  6.55    .
    2022  7.13 "TX" "GOM"  7.13    .     . 7.13
    2022  8.32 "US" ""        . 8.32     .    .
    2022  6.84 "UT" ""     6.84    .  6.84    .
    2022  7.99 "VA" ""     7.99    .  7.99    .
    2022 11.88 "VT" ""    11.88    . 11.88    .
    2022  6.17 "WA" ""     6.17    .  6.17    .
    2022  8.49 "WI" ""     8.49    .  8.49    .
    2022  6.74 "WV" ""     6.74    .  6.74    .
    2022  6.89 "WY" ""     6.89    .  6.89    .
    end
    
        graph hbar price0 price1 price01, over(state, sort(price) descending lab(labsize(tiny))) ///
    bar(1, color(black)) bar(2, color(red)) bar(3, color(blue)) ///
        ylabel(0(2)18, angle(0) labsize(vsmall)) ///
        legend(off) ///
        ytitle("$/kWh")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	65.6 KB
ID:	1732237


  • #2
    You have more problems than one.

    The most immediate problem is that you're needing the nofill option.

    A more subtle problem is that your three price variables need to be disjoint, i.e. non-missing just once in each observation, as otherwise you're asking for the same value to be shown as two bars in some cases.

    Beyond that, there are 52 observations (50 states + DC + US as a whole) so you're struggling to make this readable. Making the state labels smaller is both needed and not helping with legibility.

    A small trick is to split the observations into two columns. The columns don't have to be explained or labelled in the graph. Explaining them otherwise is up to you.

    Then -- and now questions of taste and judgment start appearing --

    The bars are a lot of ink showing that prices are not zero. This surprises no-one, least of all the economists who are presumably the audience. The point, I guess, is only to compare prices with each other. That suggests a dot chart and allows you to omit zero, giving yourself more space.

    Besides, many bars in strong colours is a bit overwhelming.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year double price str181 state str3 region double(price0 price1 price00 price01)
    2022 18.43 "AK" ""    18.43    . 18.43    .
    2022  7.72 "AL" "GOM"  7.72    .     . 7.72
    2022  7.38 "AR" ""     7.38    .  7.38    .
    2022  7.86 "AZ" ""     7.86    .  7.86    .
    2022 17.09 "CA" ""    17.09    . 17.09    .
    2022  8.63 "CO" ""     8.63    .  8.63    .
    2022 15.07 "CT" ""    15.07    . 15.07    .
    2022  7.74 "DC" ""     7.74    .  7.74    .
    2022  8.79 "DE" ""     8.79    .  8.79    .
    2022  9.16 "FL" ""     9.16    .  9.16    .
    2022  8.65 "GA" ""     8.65    .  8.65    .
    2022  7.06 "IA" ""     7.06    .  7.06    .
    2022  6.71 "ID" ""     6.71    .  6.71    .
    2022  8.57 "IL" ""     8.57    .  8.57    .
    2022  8.65 "IN" ""     8.65    .  8.65    .
    2022   8.3 "KS" ""      8.3    .   8.3    .
    2022  7.41 "KY" ""     7.41    .  7.41    .
    2022  7.54 "LA" "GOM"  7.54    .     . 7.54
    2022 17.06 "MA" ""    17.06    . 17.06    .
    2022 10.01 "MD" ""    10.01    . 10.01    .
    2022 11.03 "ME" ""    11.03    . 11.03    .
    2022  8.33 "MI" ""     8.33    .  8.33    .
    2022  9.25 "MN" ""     9.25    .  9.25    .
    2022  7.67 "MO" ""     7.67    .  7.67    .
    2022  6.71 "MS" "GOM"  6.71    .     . 6.71
    2022  7.49 "MT" ""     7.49    .  7.49    .
    2022  6.54 "NC" ""     6.54    .  6.54    .
    2022  7.28 "ND" ""     7.28    .  7.28    .
    2022  7.21 "NE" ""     7.21    .  7.21    .
    2022 15.15 "NH" ""    15.15    . 15.15    .
    2022 12.12 "NJ" ""    12.12    . 12.12    .
    2022  6.56 "NM" ""     6.56    .  6.56    .
    2022   8.5 "NV" ""      8.5    .   8.5    .
    2022  7.55 "NY" ""     7.55    .  7.55    .
    2022  7.45 "OH" ""     7.45    .  7.45    .
    2022  6.96 "OK" ""     6.96    .  6.96    .
    2022  6.81 "OR" ""     6.81    .  6.81    .
    2022  8.21 "PA" ""     8.21    .  8.21    .
    2022 17.96 "RI" ""    17.96    . 17.96    .
    2022  7.13 "SC" ""     7.13    .  7.13    .
    2022  8.04 "SD" ""     8.04    .  8.04    .
    2022  6.55 "TN" ""     6.55    .  6.55    .
    2022  7.13 "TX" "GOM"  7.13    .     . 7.13
    2022  8.32 "US" ""        . 8.32     .    .
    2022  6.84 "UT" ""     6.84    .  6.84    .
    2022  7.99 "VA" ""     7.99    .  7.99    .
    2022 11.88 "VT" ""    11.88    . 11.88    .
    2022  6.17 "WA" ""     6.17    .  6.17    .
    2022  8.49 "WI" ""     8.49    .  8.49    .
    2022  6.74 "WV" ""     6.74    .  6.74    .
    2022  6.89 "WY" ""     6.89    .  6.89    .
    end
    
    sort price
    gen which = _n <= 26
    
    replace price0 = . if price01 <  .
    
    graph dot price0 price1 price01 price, over(state, sort(price) descending) ///
    marker(1, ms(Oh) mcolor(black)) marker(2, ms(O) mcolor(red)) marker(3, ms(T) mcolor(blue)) ///
    marker(4, mcolor(none)) ///
    nofill ylabel(6(2)18, angle(0)) ysc(alt r(5.5 .)) linetype(line) lines(lc(gs12) lw(vthin)) ///
    exclude0  by(which, note("") legend(off) t1title("$/kWh")) subtitle("")

    I like very thin solid lines in grey as a grid with dot charts. I had to resort to a further trick to get them in this case.

    Whenever a graph has table flavour, as is surely true in this case, I find that an axis at the top looks good. More on this at https://journals.sagepub.com/doi/pdf...867X1201200314

    If you wanted the Gulf States and/or the US point to stand out more clearly, you could bump up the marker size.

    Naturally most of these points are suggestions, but I've answered your specific questions too.
    Last edited by Nick Cox; 01 Nov 2023, 06:01. Reason: Corrected HTML rendering of <=.

    Comment


    • #3
      Some nice suggestions, Nick Cox. Thank you very much!

      Comment

      Working...
      X