Announcement

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

  • Sorting stacked bars based on the sum of two variables

    Dear Statalisters,

    The following graph was obtained using the code below in Stata/SE 14.2. The data is on per capita households and government consumption for 20 countries in the year 2017. Is there a way to sort the stacked bars based on the sum of the two variables displayed: pc_cpd "Households consumption" and pc_cgd "Government consumption" instead of doing the sorting based on one of them?

    Code:
    graph hbar (asis) pc_cpd pc_cgd , over(country, sort(1) descending) ///
    blabel(bar, position(base) format(%4.1f)) stack nofill scheme(s1color) subtitle(, fcolor(white)) ///
    title("Per capita consumption in Arab Countries 2017") subtitle((US dollars/day)) name(consumption)

    Many thanks.
    Click image for larger version

Name:	consumption.png
Views:	1
Size:	23.0 KB
ID:	1439070

    Last edited by Imed Limam; 12 Apr 2018, 11:43.

  • #2
    Just

    Code:
    gen sorttotal = pc_cpd +  pc_cgd
    and then sort(sorttotal) rather than sort(1)

    The help explains about the possibility of sorting on a named variable.

    I would play with the aspect ratio and make the bar fill colours
    none; otherwise the numbers will be hard to see.

    Also, zap repeated "consumption" in the legend and move it to bottom right inside the plot region. You'll gain space thereby.

    Comment


    • #3
      Thank you very much Nick.

      Comment

      Working...
      X